Arun Menon
Arun Menon

Reputation: 23

How to invert shifting and addition

1E 1B 01 13 6 [ 0001 1110 0001 1011 0000 0001 0001 0011 0110 ]is converted to F6C336 by doing sifting and addition. (0x1E<<19)+(0x1B<<14)+(0x01<<9)+(0x13<<4)+6 = F6C336[1111 0110 1100 0011 0011 0110]

Now, I am stuck to reverse this calculation. i.e. From F6C336, I want to get 1E 1B 01 13 6.

Sorry for my poor knowledge in bit operations.

Upvotes: 0

Views: 122

Answers (1)

user555045
user555045

Reputation: 64904

If these are four blocks of 5 bits each and one block of 4 bits each, then the "conversion" is their concatenation, and the inverse of that is splitting it back up into those pieces. For example:

piece0 = x >> 19;
piece1 = (x >> 14) & 31;
piece2 = (x >> 9) & 31;
piece3 = (x >> 4) & 31;
piece4 = x & 15;

Shown in Java here but the logic would be similar in most languages.

If the input was not of that form, for example if it was FF FF FF FF F then the inverse is ambiguous.

Upvotes: 0

Related Questions