How to make two 8bit numbers from a 10bit number?

Hi group,

I have a ten bit number that I need to split into two 8 bit numbers. The first 8 bit number needs to have 3 zeros followed by the first 5 bits of the 10 bit number. The second 8 bit number needs to start with the last 5 bits of the 10 bit number, followed by 3 zeros. Like the example below:

Original 10 bit number: B1101111101 Turn this into: B00011011 & B11101000

This is for a serial transmission (SPI) program I'm working on.

Any suggestions?

Thanks, Patrick


I haven’t tested this, but I think it will work:

int tenbits = 0x37d;  // B1101111101
byte b1;
byte b2;

b1 = (tenbits >> 5) & 0x1f;
b2 = (tenbits << 3) & 0xf8;



Hi Mike,

Thanks for the suggestion. I'll give it a try.

I was thinking a bitshift operation would be the way to go, but I hadn't thought about &'ing it with an 8 bit number to get an 8 bit result. That's the key.