amir
amir

Reputation: 13

How to use remained bits of a byte to store value in it?

All of us know that Integer datatype can store values till 4 bytes, means 32 bits

so for example if we have

int a = 2;

it means a = 00000000 00000000 00000000 00000010

Is there any way to use the remained bits and store value in them and take them out when needed?

Let me take another example:

We have a computer system that only uses English alphabets and numbers (26 + 10), so the sum of them is 36.

Like when when we have 256 characters in computer and log_2(256) = 8 bits and we use 8 bits to store values

log_2(36)= 6 bits it means that 6 bit is enough for the values.

Here is the question:

how I can use only three bytes to store 4 characters in it?

base on log2(36) = 6

this photo can show the idea better

Upvotes: 1

Views: 339

Answers (1)

Andrew F
Andrew F

Reputation: 2950

Base64

The Base64 encoding is exactly what you want, and it has Java implementations. From the Wiki

Each Base64 digit represents exactly 6 bits of data. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit Base64 digits.

Java8's docs include Base64 here.

Old, Unnecessarily Complicated Answer

To answer your question in particular, you can cram four characters into three bytes using bit shifting and a custom binary encoding.

For example, if you were to encode the lowercase letters of the alphabet and digits as six-bit codes

Symbol  | Decimal | Binary
0       | 0       | 000000
1       | 1       | 000001
...
f       | 16      | 010000
...
o       | 25      | 011001
...
z       | 35      | 100011
...
EOF     | 63      | 111111

Then the string foof could be written as 010000 011001 011001 010000. Most languages would prefer to represent this as three bytes, or 01000001 10010110 01010000. This is A搀 using the utf-8 binary encoding or -63 22 -48 if you represent them as the byte type in Java.

I'm not terribly familiar with Java, but I know there are several resources for C and other languages to read off the exact bit-values for some bytestream.

Upvotes: 1

Related Questions