Deep dive into bits, bytes, shorts, ints, longs, signed, and unsigned with Java

On the Pi4J discussion list, someone recently asked what the best and easiest way is in Java to convert a byte value . In Java, there is no distinction between signed and unsigned bytes, which can be confusing. My book “Getting Started with Java on the Raspberry Pi” contains an explanation about this, and I am happy to share it in this post with some more info and code examples…

You can find all the code of this post on GitHub .

The Basics: Bits

Let’s start with the basics: bits, 0 or 1.

A bit (binary digit) is the smallest unit of data in a computer, and has two possible values: 0 or 1. Bits are the foundation of everything that happens in a computer. All instructions or data stored in memory are represented as combinations of bits. Bits are mostly grouped together into larger units such as bytes or words, so they can represent numbers, characters, or control signals, depending on the context in which they are used.

In everyday life, we are used to decimal values where we group everything by 10, 20, 30,… In programming, hexadecimal (or hex) values are often used, which group numbers by sixteen. Hexadecimal values range from 0 to 15, which matches perfectly with the maximum value of four bits (1111 in binary). Each hex digit can represent a value from 0 to F, where F is 15 in decimal. A hex value is typically written as 0x0 to 0xF to distinguish it from decimal notation.

Each binary digit (bit) represents a power of 2, starting from the rightmost bit (which is 2^0) and moving left. The value of the binary number is the sum of the powers of 2 where there is a bit 1.

The following table shows all possible combinations of 4 bits ranging from “0000” to “1111”.

Bits2^32^22^12^0+NumberHEX
8421
000000000+0+0+000x0
000100010+0+0+110x1
001000100+0+2+020x2
001100110+0+2+130x3
010001000+4+0+040x4
010101010+4+0+150x5
011001100+4+2+060x6
011101110+4+2+170x7
100010008+0+0+080x8
100110018+0+0+190x9
101010108+0+2+0100xA
101110118+0+2+1110xB
110011008+4+0+0120xC
110111018+4+0+1130xD
111011108+4+2+0140xE
111111118+4+2+1150xF

This video by Mathmo14159 very nicely illustrates how this works.

And we can achieve the same result with the following Java code:

System.out.println("Value\tBits\tHex");
for (int i = 0; i <= 15; i++) {
    System.out.println(i 
        + "\t" + String.format("%4s", Integer.toBinaryString(i)).replace(' ', '0')
        + "\t0x" + Integer.toHexString(i).toUpperCase());
}

// Output
Value   Bits    Hex
0       0000    0x0
1       0001    0x1
2       0010    0x2
3       0011    0x3
4       0100    0x4
5       0101    0x5
6       0110    0x6
7       0111    0x7
8       1000    0x8
9       1001    0x9
10      1010    0xA
11      1011    0xB
12      1100    0xC
13      1101    0xD
14      1110    0xE
15      1111    0xF

Bits to Byte

A byte consists of 8 bits and has the range of 0x00 (= 0) to 0xFF (= 255).

So we need to extend the table above to have 8 bits. Let’s take a few examples:

Bits2^72^62^52^42^32^22^12^0+TotalHEX
1286432168421
000000010000000111x01
000000100000001022x02
00000011000000112+13x03
000001000000010044x04
00001111000011118+4+2+115x0F
000111110001111116+…+131x1F
00100000001000003232x20
11111111111