I want to construct a key composed of 3 values by using bit shifting operations:
According to my understanding, the C statement code I am starting from creates a hash table by constructing its keys from certain data variables:
uint64_t key = (uint64_t)c->pos<<32 | c->isize;
My interpretation is that key
is a combination of the last 32 digits
of c->pos
, which must be a 64 bit unsigned integer, and c->isize
, also a 64bit unsigned integer.
But I am not sure if that is the case, and maybe the |
pipe operator
has a different meaning when applied to bit shifting operations.
What I want to do next is to modify the way key
is constructed and
include a third c->barc
element into the variable. Given the number
of possibilities of c->barc
and c->isize
, I was thinking that instead
of building key
with 32+32 bits (pos
+isize
), I would build it
with 32+16+16 bits (pos
+isize
+barc
) splitting the last 32 bits between
isize
and barc
.
Any ideas how to do that?
What I think you need is a solid explanation of bitmasking.
For this particular case, you should use the & operator to mask out the upper 16 bits of c->isize before shifting it up, and then use the & operator again to mask the upper 48 bits of c->barc.
Let's look at some diagrams.
If I understand correctly, you want a 64-bit number like this:
right?
As you already know, we get the upper 32 x's by doing
Now, we want to bitwise-or that with the following:
To get that number there, we do this:
Now, the final part, the
is the result plain and simply of
So we take all of these expressions and bitwise-or them together.