Bit Manipulation

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
Kumaran
Posts: 73
Joined: Tue Apr 08, 2008 5:23 pm
Been thanked: 1 time
Contact:

Bit Manipulation

Post by Kumaran »

I want to extract single bit data from the two bit data. How to do that? I have written a following code which is assigning two bit data to the varibles Left & Right. But I wanted it to be single bit data. only then my rest of the code will work as I wanted.

Left = New AND 0x0A ( New is two bit variable from that I want to extract the left bit and assign to the Varaible Left)
Right = Old AND 0x01 ( Old is two bit variable from that I want to extract the right bit and assign to the Varaible Right)
Direction.Return = Left XOR Right

Could you please suggest me how to do that?

Regards
Kumaran

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Bit Manipulation

Post by Benj »

Hello

Ok to start the microcontrollers use 8 bit variables as a minimum,

So lets say you have 2 variables

IN - contains the following bits Xxxxxxxx - We are interested in the large X bit

OUT = IN & 0x80 - Masks IN to provide OUT with the value X0000000

If we want to shift this single bit to the least significant place then we would do this

OUT = OUT >> 7

OUT then equals 0000000X


Ok so another example.

IN - contains the following bits xxxXXXxx - We are interested in the large X bit

OUT = IN & 0x1C - Masks IN to provide OUT with the value 000XXX00

If we want to shift these 3-bits to the least significant place then we would do this

OUT = OUT >> 2

OUT then equals 00000XXX

Hope this helps.

Post Reply