PDA

View Full Version : Accessing several bits together in a byte or word...



rossfree
- 7th May 2007, 17:12
I know there is a way... but can't find it in a search and can't remember how to do it...

I would like to insert a three-bit number into a byte or word such as...

Put 110 into byte xxxxxxx and get xxxx110x.

I know how to access single bits, low and highbytes... etc. But I need to REPLACE several bits in a byte in one command.

Thank you,

Ross

skimask
- 7th May 2007, 18:01
I know there is a way... but can't find it in a search and can't remember how to do it...
I would like to insert a three-bit number into a byte or word such as...
Put 110 into byte xxxxxxx and get xxxx110x.
I know how to access single bits, low and highbytes... etc. But I need to REPLACE several bits in a byte in one command.
Thank you,
Ross

oldval var byte
newval var byte

oldval = %01010101 'just an example value
newval = %110 'but you don't want this into the lower 3 bits, you want it in bits2,3,4...
newval = newval << 1 'shift the newval bits left one position
oldval = oldval AND %11110001 'clear out old bits that you want to replace
oldval = oldval | newval 'bitwise OR to set the bits

It's not one command...I don't think you'll ever get away with trying to use one command. Too many situations, too many (pardon the pun) variables.

rossfree
- 7th May 2007, 18:07
Yep... that should do it.

Thank you Skimask

Ross