user5156141
user5156141

Reputation: 684

Convert 12-bit number into 2 8-bit bytes

For the life of me I cannot figure out how to convert a (maximum) 12-bit number into 2 8-bit bytes (where one of the values will obviously not be able to exceed 4-bits).

For example, I can convert 7 (the 4-bit value) and 60 (8 bit value) integers into 3079 via:
7 + (60 << 8) & 0xfff;

But I cannot reverse this process, extracting 60 and 7 FROM 3079.

I use the following for 16 bits, but I cannot figure out how to modify it for 12-bit values as they do not result in the original value:

(calcMSB << 8) | (calcLSB & 0xff)

Upvotes: 0

Views: 1396

Answers (1)

VillageTech
VillageTech

Reputation: 1995

Mainly there is no difference between 16 bits and 12 bits here. See pseudocode below:

calcLSB = value12bit & 0xFF; // get LSB byte
calcMSB = value12bit >> 8;    // shift bytes over LSB and take them

Upvotes: 0

Related Questions