Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

CS 101 – Binary and Hexadecimal

You may have heard the old programmer’s joke:

There are 10 kinds of people in this world:

Those who understand Binary,

And those who don’t.

This is an excellent introduction into the topic.

Binary

Computers, being dumb machines, are composed primarily of a host of switches. These switches have two states: ON and OFF (or CLOSED and OPEN). When the switch is in the ON/CLOSED state, electricity is able to flow through it. When the switch is OFF/OPEN, electricity cannot flow.

Exception: due to the nature of the materials, enough electrical current can override the open state – this is the result of a SHORT.

We tend to record all ON states as a 1, and all OFF states as a 0. A single value ON or OFF is called a BIT.

Thus, we create a mathematical system composed of only two digit values.

You might recall from elementary school that the decimal (10 digit) system has “place values”, based on powers of 10. Thus, the decimal value 12345 breaks down to the following:

1 * 10^4   +   2 * 10^3   +   3 * 10^2   +   4 * 10^1   +   5 * 10^0

We do the same thing in binary, using powers of 2. Thus, the binary value 10011101 breaks down to the following:

1 * 2^7   +   1 * 2^4   +   1 * 2 ^ 3   +   1 * 2^2   +   1 * 2^0

This converts to the decimal value 128 + 16 + 8 + 4 + 1 = 157.

For programmers, it is crucial to learn to instinctively convert between binary and decimal.

Hexadecimal

While we can learn to read binary, strings like this become difficult to understand:

10010110110101010001000011111010…

We make this easier to understand by breaking it up into groups of four, as such:

1001  0110  1101  0101  0001  0000  1111  1010

This is easier to read, because we can break it into decimal relatively easily, but brilliant minds conceived of a better way. Consider that these four-bit chunks can store a total of 2^4 values, in the range between 0 and 15. Enter hexadecimal, a number system based on 16 possible digit values.

0   1   2   3   4   5   6   7

0   1   2   3   4   5   6   7 

8   9   10  11  12  13  14  15

8   9   A   B   C   D   E   F

Using the above chart (decimal on top, hexadecimal on bottom), we can convert our example string into the following:

9   6   D   5   1   0   F   A

Much easier to read, no?

Representation

Knowing what you do now, what is the value of 1001?

Conventionally, you have to assume that this is a decimal value, but the fact is, you can’t tell from that whether I wrote it in bin(ary), hex(adecimal), or dec(imal).

Below is the value, using common notation:

Value                    Decimal

0b1001                        9

0x1001                     4097

1001                      1001

As you can see above, 0b tends to indicate binary, while 0x tends to indicate hexadecimal. There are other notation systems, but these are among the most common and useful.

Aside: Octal

There is one more (somewhat) common representation called octal. This is a system based on three-bits (e.g. 011), and can store values from 0 to 7.

Below is an example of octal.

0o1234          =       0b0010 1001 1100    = 0x29C   =   668

There is not a lot of call for octal, because it’s easiest for us to work in multiples of 2, but here it is.

Homework

If you really want to become a good programmer, you will have to learn the powers of 2 up through 65536 (2^16). Practice counting by multiples of 2.

Also, write out random strings of binary (0 and 1), break them into fours, and convert them to hexadecimal.

Then, take the same hexadecimal numbers and convert them to decimal.

 

Sadly, this is like learning elementary math. The only way to master it is by rote repetition.