Page 1 of 1

Can I shift bit 4 over to bit 3 without shifting bits 0-2

Posted: Thu Jul 23, 2009 4:02 pm
by DanDMan
I have to read a 4 bit binary value from ra0,ra1,ra2, ra4. If I read on only those bits, can I shift bit 4 over to bit 3 without effecting bits 0 to 2?

The only other way I could think of is to read bits into a variable and bit 4 into another variable, then shifting one var and then using OR to combine them into my 4 bit value. Any ideas or suggestions?

Re: Can I shift bit 4 over to bit 3 without shifting bits 0-2

Posted: Fri Jul 24, 2009 8:17 am
by Benj
Hello

Yes you can do this quite easily.

Simply read the port into a variable using masking so you are only reading the four bits you are interested in - 0,1,2,4.

The using a decision check if the 4th bit is set.

var > 0x0F

If yes then using a calculation icon do the following.

var = var | 0x08
var = var & 0x0F

This simply sets the 3rd bit and then masks the variable so that only the bottom 4 bits are allowed to be set.

If no then you do not have to worry as the 3rd bit is already clear.

Hope this helps.