A general Bit manipulation question

Hello,
I have a general bit manipulation question.

Lets say we have three bytes
Byte_A
Byte_B
Byte_C

We want to copy certain bit values from Byte_A into Byte_B and Byte_C

First, we want to take the top 4 most significant bit values in Byte_A
And we want these to be duplicated within the bottom 4 least significant bits in Byte_B.
But without altering Byte_B's top 4 most significant bits

Then we we want to take the bottom 4 least significant bits form Byte_A
And we want these to be duplicated within the bottom 4 least significant bits in Byte_C.
But again without altering Byte_C's top 4 most significant bits

Whats is the Arduino way of doing this?

byteAupper = byteA && 0xF0; // clears lower bits
byteAlower = byteA && 0x0F; // clears upper bits

byteBlower = byteB && 0x0F;
byteAupperbyteBlower = byteAupper || byteblower; // combine an upper and a lower

byteCupper = byteC && 0xF0;
byteCupperbyteAlower = byteCupper || byteAlower;

make sense?

If you want upper bits to end up in lower location, you can clear them and then shift them left or right.

byteAupperMoved = (byteA && 0xF0) >> 4;

byteAlowerMoved = (byteA && 0x0F) << 4;

And then OR ( || ) in them in like above.

Thank you CrossRoads!
Let me play with these and get back to you if I have questions.
Otherwise - thank you very much!

CrossRoads:
byteAupper = byteA && 0xF0; // clears lower bits
byteAlower = byteA && 0x0F; // clears upper bits

byteBlower = byteB && 0x0F;
byteAupperbyteBlower = byteAupper || byteblower; // combine an upper and a lower

byteCupper = byteC && 0xF0;
byteCupperbyteAlower = byteCupper || byteAlower;

make sense?
&& - Arduino Reference
|| - Arduino Reference

spot on!
great answer
congrats

On the first step:
byteAupper = byteA && 0xF0; // clears lower bits

I tried this:
Byte_A = 255;
Byte_AUpper = 0;

Byte_AUpper = Byte_A && 0xF0; // clears the lower bits
Serial.println(Byte_AUpper);

Instead of clearing the lower bits and returning xF0 (decimal 240) it returned 0x1

And this also returned the value 0x1

Byte_AUpper = Byte_A && 0x0F;

Try Serial.println(Byte_AUpper, HEX);

I don't what you'd get trying to print the character that 240 represents from the extended ascii codes.

&& performs a logical AND ( && - Arduino Reference ) and will always return 0 or 1.

You must use a bitwise AND, see & - Arduino Reference .

Erik_Baas:
&& performs a logical AND - - - you must use a bitwise AND, see & - Arduino Reference .

AH!
Yes!
Thanks

Rats, did I get those backwards? & vs &&, and | vs || ?

It's a bit funky but there's insert_bits.

Thank you very much!
I'm pretty sure I've got it!
I'm going to run some further tests to verify consistency - but I think this is it.

To copy Byte_A's upper 4 bits into Byte_B without altering Byte_B's 4 top bits

//clear Byte_B's lower 4 bits

  1. Byte_B = Byte_B & 0xF0;

//Take Byte_A's upper 4 bits and shift them down & load into TempBits
2) TempBits = Byte_A >> 4;

//Now OR Byte_B with TempBits
3) Byte_B = Byte_B | TempBits;

To Copy Byte_A's lower 4 bits into Byte_C without altering Byte_C's 4 top bits

//clear Byte_C's lower 4 bits

  1. Byte_C = Byte_C & 0xF0;

//Load Byte_C into TempBits with the 4 upper bits cleared
2) TempBits = Byte_C & 0x0F;

//Now OR Byte_C with TempBits
3) Byte_C = Byte_C | Temp4bits;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.