i'm writing a program in c++ where i have registers. they are 8 bit each. however, i may have values bigger than 255, in which case i need to place the value in say 2 registers if it has 16 bits, 3 registers for 24 bits etc.
so i need to extract first 8 bits (which for me are msb) and second 8 bits (lsb).
getting lsb didn't require me much of thinking:
Code:
register = value & 0x00FF
for msb, this is what i come up with:
Code:
register = (value & 0xFF00) >> 8;
it works but i'm here to ask if there's a better solution.
hmm. are those 2 really better solutions? i mean, bitwise operations are much faster than any other operation, let alone a for loop executing 16 times.
and i still don't get what is the purpose of this array or union. my register storage is itself an array of bytes.
As Easyrider83 mentioned, by handling the variable of 2 bytes length as an array, you can read one byte without the need of any shift opperation, as for example if you declare this: