Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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.
Updated 8-May-19 16:52pm
[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.

of course you can store 8 integers in a byte. but those integers can only have values 0 or 1.
Share this answer
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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900