Nov 22, 2022 #1 E ElecDesigner Member level 5 Joined Jul 22, 2014 Messages 94 Helped 4 Reputation 8 Reaction score 5 Trophy points 1,288 Activity points 2,190 Is there a way in C to move individual binary bits from one int to another. I.E. say bit 3 of int a to bit 7 of int b etc. It would be handy for mapping enable bits to various pins on a specific port on a micro.
Is there a way in C to move individual binary bits from one int to another. I.E. say bit 3 of int a to bit 7 of int b etc. It would be handy for mapping enable bits to various pins on a specific port on a micro.
Nov 22, 2022 #2 danadakk Advanced Member level 6 Joined Mar 26, 2018 Messages 3,112 Helped 414 Reputation 848 Reaction score 729 Trophy points 113 Activity points 13,479 Do extract source bit with AND mask and then set/clr bit in destination with AND mask ? Or use inline ASM code in a subroutine to do it, where, depending on processor, you have set/clr/read bit instructions..... Or a.A macro in C...... Regards, Dana. Upvote 0 Downvote
Do extract source bit with AND mask and then set/clr bit in destination with AND mask ? Or use inline ASM code in a subroutine to do it, where, depending on processor, you have set/clr/read bit instructions..... Or a.A macro in C...... Regards, Dana.
Nov 22, 2022 #3 S scopeprobe Full Member level 2 Joined Sep 17, 2020 Messages 147 Helped 27 Reputation 58 Reaction score 85 Trophy points 28 Location Uk Activity points 1,626 ElecDesigner said: Is there a way in C to move individual binary bits from one int to another. I.E. say bit 3 of int a to bit 7 of int b etc. It would be handy for mapping enable bits to various pins on a specific port on a micro. Click to expand... Yes, use the | (LOGICAL OR) operator unsigned char a, b; a = 0x00; //Initialise Variables b = 0x00; a = 0x04; // Set bit 3 a = a << 4; // Shift bit 3 to bit 7 b = b | a; //do a OR function which will set variable b bit 7 Upvote 0 Downvote
ElecDesigner said: Is there a way in C to move individual binary bits from one int to another. I.E. say bit 3 of int a to bit 7 of int b etc. It would be handy for mapping enable bits to various pins on a specific port on a micro. Click to expand... Yes, use the | (LOGICAL OR) operator unsigned char a, b; a = 0x00; //Initialise Variables b = 0x00; a = 0x04; // Set bit 3 a = a << 4; // Shift bit 3 to bit 7 b = b | a; //do a OR function which will set variable b bit 7
Nov 22, 2022 #4 barry Advanced Member level 7 Joined Mar 31, 2005 Messages 6,333 Helped 1,194 Reputation 2,400 Reaction score 1,389 Trophy points 1,393 Location California, USA Activity points 34,481 If they’re enable pins, i.e. bits, why do you have them in an int? Upvote 0 Downvote