You can use bitwise operators to achieve this. Let's say you want to sacrifice the 8 most significant bits in your long long. (Be careful if your long long is negative ! The sign is stored as the most significant bit, so you'll lose the sign !)
Now, to do this, you can do :
byte get_hp_id(int64 global_row_id)
{
return ((char)global_row_id);
}
int64 get_row_id(int64 global_row_id)
{
return (global_row_id >> 8);
}
int64 make_global_rowid(int64 rowid, byte hp_id)
{
return (rowid << 8 + hp_id)
}
For the little explanation, <<
is a bitshifting operator. What it does is that it shifts all your bits either right or left. Bits that goes out of boundaries are lost, and bits coming from nowhere are set to 0 :
1001001 << 2 == 0100100 // the first 1 is lost, and 2 "0" come from the right
In your case, we shift to the right of 8 bits (to leave space for your byte), and therefore the 8 most significant bits are lost forever.
But now, we have something like :
(xxxxxxxx)(xxxxxxxx)(xxxxxxxx)(00000000)
Which means that we can add anything fitting in 8 bits without modifying the original value. And tada ! We have stored a byte in a long long !
Now, to extract the byte, you can just cast it as a char. During the cast, only the 8 least significant bits are saved (your byte).
And finally, to extract your long long, you just have to shift the bits the other way around. The byte will be overwritten, and your long long will be as good as new !