The general idea is to mask part of the bits using bitwise boolean operations and the shift operator >>
to move the bits to the desired location.
Specifically the &
operator allows you to let specific bits as they are, and set all others to zero.
As in your example for y
you need to mask bits 11-21 from the 32 bits of the input like in:
input xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
& with 00000000000111111111110000000000
= 00000000000xxxxxxxxxxx0000000000
shift by 10 bits to the right
= 000000000000000000000xxxxxxxxxxx
Since you cannot write bits in C style, you have to convert the bitmask to hex.
The conversion to floating point is handled inplicitly, when you assign it a double or float variable, assuming you want to convert the integer-value (e.g. 5) to floating point (e.g. 5.0).
Assuming that your bits are partitioned as
xxxxxxxxxxxyyyyyyyyyyyzzzzzzzzzz
and you want double results your example would then be as following:
int v = 0; //input
double x = v >> 21;
double y = (v & 0x1FFC00) >> 10;
double z = v & 0x3FF;