Page 1 of 1

Bit Manipulation

Posted: Fri Aug 22, 2008 7:48 am
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

Re: Bit Manipulation

Posted: Fri Aug 22, 2008 5:33 pm
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.