Section 3 - Binary and Hex

3A.3.1 The Binary Number System

At the machine level, all data are stored as binary representations. In order to work with data at this level, we need to understand what a binary representation is.

Decimal or Base 10

We begin by examining the numbers we know, ordinary decimal numbers. Pick a number, any number. 3095 you say? Fine. While we think we know what this number means, we might balk if asked to explain it. One explanation is that each digit, the 3, the 0, the 9 and the 5, represent a portion, or contribution, to the full number. Working from right-to-left:

Note

When we say "there are zero 100s in 3095", we are not denying that there are lots of 100s making up the number 3095. We mean, rather, that if you first count all the larger power of 10 -- the thousands, the 10 thousands, etc. (in this case that would come to 3000), then there are no 100s left over, which as you can see, there are not. After you account for the 3 thousands there is only 95 left over, and this does not contain any 100s.

If you add it all up, you get:

5*1 + 9*10 + 0*100 + 3*1000 = (no surprise) 3095.

So you see, that the position of each digit, when measured from the right end of the number, represents an increasing power of 10.

3095 as Properly Intepreted in Decimal
104(and beyond) 103 102 101 100
All 0, so no need to write leading 0s 3 0 9 5
"10 thousands place (and beyond)" "thousands place" "hundreds place" "tens place" "1s place"

(In case you didn't know, 100 = (anything non-zero)0 = 1.)

We call this base 10 because each position of a number represents a power of 10, acting as a base to the exponent. You don't even think about it because you've been using base 10 since you were old enough to ask for a new toy.

Binary or Base 2

At the microscopic level, computer memory consist of bits, each of which can be in only one of two states (these are usually one of two charge states in a very tiny capacitor). This means we can't store numbers in decimal format because we cannot represent a 5 vs. a 3 vs. a 9 in a bit. We can only represent two values: 0 or 1. This is why computers use binary numbers.

The binary system is like an super-simple decimal system. Instead of ten digits, 0-9, there are only two digits, 0 and 1. Each position of a binary number represents a power of 2. So, the number 1001 can be understood using the same table as above, but using 2 as a base, rather than 10. Again, from right-to-left:

If you add it all up, you get:

1*1 + 0*2 + 0*4 + 1*8 = 9.

So, in the binary system 1001 represents the number 9 (not the number one thousand and one). Here is the tabular representation:

9 Represented in Binary
24(and beyond) 23 22 21 20
All 0, so no need to write leading 0s 1 0 0 1
"16s place (and beyond)" "eights place" "fours place" "twos place" "1s place"

We usually call the places or positions in a binary number bits. Because we only get to use a 0 or 1 in each bit, it takes more digits to represent the same number in binary than it does in decimal. For example the number 129 in binary requires eight bits:

129 decimal = 10000001 binary

This is easily seen by noticing that the eighth place from the right is the 128s place (start with 1 and double it 7 times). So the number is 1*128 + 1 = 129.

Eight bits has a special name in computation: the byte.

Here are some binary and decimal equivalents:

Some Binary-Decimal Equivalents
decimal binary
3 11
21 10101
32 100000
137 10001001
255 11111111

3A.3.2 Hexadecimal Notation

Hexadecimal or Base 16

In base 16, also called hexadecimal, each position represents a power of 16. That means we need 16 digits for each place. Of course the digits 0-9 are good for the first 10 but what about the next six? We use the characters A - F. So A represents 10, B = 11, C = 12, and so on up to F = 15. This means that we can use fewer positions in hex to represent the same number in decimal. For example D (hex) = 13 (decimal). And FFF (hex) = 4095 (decimal)!

The hexadecimal number (or hex number, for short) 2DA1 can be analyzed. Again, from right-to-left:

If you add it all up, you get:

1*1 + A*16 + D*256 + 2*4096 = 11681.

So, in the hexadecimal system 2DA1 represents the number 11681. Here is the tabular representation:

2DA1 Interpreted as Hex
164(and beyond) 163 162 161 160
All 0, so no need to write leading 0s 2 D A 1
"65,536s place (and beyond)" "4096 place" "256s place" "16s place" "1s place"

Sometimes programmers use hex when they really want to enter a binary number, because hex is easier on the eyes. It is easy to convert a binary number to a hex and back: just group the binary number into clumps of 4 digits:

1010 0011 0111 1000 binary = A 3 7 8 hex

You can write a hex literal in C++ and Java using the prefix 0x before the number:

 int x = 0x2DA1; // the decimal value happens to be 11681 as we saw
int y = 0xA378;  // the binary value happens to be 1010001101111000 as we saw
// now print x and y to see the decimal values

Note that we are not discussing hex or bin output, so the result of the output will be decimal.

We won't use hex in what follows, but it is important to know that it is seen and needed in programming frequently.