C Binary Bits to Binary Bits

Status
Not open for further replies.

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.
 

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.
 
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.

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
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…