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,540 Helped 426 Reputation 875 Reaction score 807 Trophy points 113 Activity points 15,149 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 3 Joined Sep 17, 2020 Messages 153 Helped 27 Reputation 58 Reaction score 90 Trophy points 28 Location Uk Activity points 1,778 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,565 Helped 1,206 Reputation 2,424 Reaction score 1,436 Trophy points 1,393 Location California, USA Activity points 35,700 If they’re enable pins, i.e. bits, why do you have them in an int? Upvote 0 Downvote