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

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
DanDMan
Posts: 11
Joined: Wed Jul 15, 2009 8:28 pm

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

Post 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?

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: Can I shift bit 4 over to bit 3 without shifting bits 0-2

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

Post Reply