Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Converting from Decimal to Hex

Binary

Binary is base 2, so we have just two symbols: 0 and 1.

\[ \begin{array}{|c|c|r|} \hline \text{decimal} & \text{hexadecimal} & \text{binary} \\ \hline 0 & 0 & 0 \\ 1 & 1 & 1 \\ 2 & 2 & 10 \\ 3 & 3 & 11 \\ 4 & 4 & 100 \\ 5 & 5 & 101 \\ 6 & 6 & 110 \\ 7 & 7 & 111 \\ 8 & 8 & 1000 \\ 9 & 9 & 1001 \\ 10 & A & 1010 \\ 11 & B & 1011 \\ 12 & C & 1100 \\ 13 & D & 1101 \\ 14 & E & 1110 \\ 15 & F & 1111 \\ \hline \end{array} \]

A binary digit is called a bit.

Notice that with 4 bits, we have \(2^4 = 16\) possibilities:

\[ \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \]

And with 8 bits, we have \(2^8 = 256\) possibilities:

\[ \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \underset{2}{\fbox{ 0/1 }} \]

A byte is 8 bits (and sometimes 4 bits is called a nibble).

We seen that there are 256 possible bytes, corresponding to the decimal values \(0 - 255\), which correspond to the binary values \(00000000 - 11111111\), which correspond to the 2 digit hex values \(\text{0x}00 - \text{0x}FF\).


Next: