Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top