reversing bitwise operations

Status
Not open for further replies.

bossredman

Well-known member
Hi - so if I have an "value" which I split into 3 x 7 bit bytes via the following code:

Code:
// Calculate the 3 "Parameter Value" bytes for sysex msg.
            byte First_byte = (value & 0x7F);
            byte Second_byte = ((value >> 7) & 0x7F);
            byte Third_byte = ((value >> 14) & 0x7F);

Is it possible to calculate the original "value" from the 3 x 7 bit bytes??

Probably a stupid question but I struggle with all this bitwise stuff.

Thanks
 
Without testing I'd say this should work:

uint32_t value = (((uint_32_t)Third_byte) << 14) | (((uint32_t) Second_byte) << 7) | (uint32_t)First_byte;
 
If you need to do this re-assembly in some high fluting language that doesn't support bit shift you can also do it less efficiently by adding the results of first_byte,second-byte*128 and 16384*third_byte to get the same result.
 
If you need to do this re-assembly in some high fluting language that doesn't support bit shift you can also do it less efficiently by adding the results of first_byte,second-byte*128 and 16384*third_byte to get the same result.

Yes, and if the compiler is smart (like GCC) it translates these 2^n multiplications to bitshifts, if it thinks they are more efficient.
 
Last edited:
Status
Not open for further replies.
Back
Top