15,907,687 members
See more:
Hello, is it possible to store 8 integers in range 0 - 31 in single byte?
I understand how to split one 32 bit integer into 4 bytes using bit shifting,
but I don't know if the same is possible with single byte.
One byte can hold 1 number up to 255. In case of 2 numbers,
I a assume they could be up to 127... in case of 8, looks like up to 31
should be real.

Or am I completely wrong and this is not possible?

I would like to have something like:

C++
```int getAt(int pos, byte b) // pos is 0 - 7
{
// extraction code
}

void setAt(int pos, byte & b) // pos is 0 - 7
{
// insertion code
}```

What I have tried:

I have tied only to search on the web and shifting like b >> 1, but without luck.
Posted
Updated 8-May-19 16:52pm
v3
[no name] 8-May-19 19:51pm
You can store 8 integers in a single byte, but they would be limited to 0 or 1.

This could be a key to something "higher" though.

e.g. 111111, the "Heaven" hexagram; made up of the dual heaven trigrams: 111; and so forth.

## Solution 2

of course you can store 8 integers in a byte. but those integers can only have values 0 or 1.

## Solution 3

Quote:
Or am I completely wrong and this is not possible?

It is not possible.
To encode a number between 0-31, you need exactly 5 bits, so to encode 8 of them in a way you can retrieve them, you need 40 bits.