
Page 1 of 2 One of the very first things that you should learn if you are dealing with programming of any variety is the concept of binary and hexadecimal number systems. The foundation of computers is based in binary—1s and 0s—and its cousin hexadecimal. Everything from HTML/CSS color codes to URL encoding to IP addresses to memory/hard drive capacity and everything in between exposes the concept of binary and hexadecimal With a basic understanding, working with computers is a bit easier.
We all use the decimal—base 10—number system. We count from 0 to 9, then begin over with 10 when we run of of digits. There are nine digits plus zero in our system of counting, and everyone knows how to do basic arithmetic with base 10 numbers. The numbers are actually symbols representing digits in the decimal system. Different number systems have different sets of symbols used to count.
Computers don't understand concepts as complex as that. They understand 0s and 1s—binary, or base 2. A binary number can be considered as "on" (1) or "off" (0). These binary digits (also called "bits") can be grouped for easier use by humans, and for easier transportation through the computer. You say a bit is "set" when the value is 1.
Early personal computers used 8-bit processors—8 of these binary digits could be moved simultaneously. Newer computers use 32-bit or 64-bit processors. These processors can move data much more quickly.
In binary, you count from 0 to 1, then begin over with 10 because you ran out of digits. This is not "ten", but "one-zero", or decimal equivalent to "2". To count from 0 to 15 in binary, you would count like this:
Dec Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
As you can see, you can store numbers from 0 to 15 (16 different values) in 4 bits. In decimal, each digit from right to left represents one power of ten: first digit is 100 or 1; second digit is 101, or 10; third digit is 102 (10*10), or 100; fourth digit is 103 (10*10*10), or 1,000; fifth digit is 104 (10*10*10*10), or 10,000; and so on. In binary, this works the same way: each digit from right to left represents one power of two: first digit is 20 or 1; second digit is 21, or 2; third digit is 22 (2*2), or 4; fourth digit is 23 (2*2*2), or 8; fifth digit is 24 (2*2*2*2), or 16; and so on.
Note: Any number raised to the power of 0 is 1. Any number raised to the power of 1 is the number itself.
We have a shorthand way of describing 4 bits of data—hexadecimal notation. The hexadecimal—base 16—number system uses symbols 0-9 and A-F as its digits. This gives us 16 separate digits. We count in hexadecimal from 0 to F and then begin over with 10. This is not "ten", but "one-zero", or decimal equivalent to "16". To count from 0 to 15 in hexadecimal, you would count like this (with the decimal and binary equivalents listed):
Dec Binary Hex
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
You can see that the hexadecimal numbers correspond to the binary and decimal numbers shown. In hexadecimal, each digit from right to left represents one power of sixteen: first digit is 160 or 1; second digit is 161, or 16; third digit is 162 (16*16), or 256; fourth digit is 163 (16*16*16), or 4,096; fifth digit is 164 (16*16*16*16), or 65,536; and so on. You can store values from 0 to 65,535 using 4 hexadecimal digits.
When you group together 8 bits, it is also known as a byte. Here is a sampling of a few different numbers from 0 to 255:
Dec Binary Hex
1 0000 0001 01
...
16 0001 0000 10
...
32 0010 0000 20
...
64 0100 0000 40
...
128
1000 0000 80
...
170
1010 1010 AA
...
255 1111 1111 FF
As you can see, the bigger the numbers get, the harder it is to figure out the binary number equivalent unless you know the trick—each group of 4 corresponds to a hexadecimal digit, and each group of 4 counts from 0 to 15 in the same way. Knowing that, you can translate any binary number into hex, or into the decimal equivalent:
1010101010010010
First, break it into groups of 4:
1010 1010 1001 0010
Then translate the numbers into hex and decimal:
AA 92 or 170, 146
Then multiply the decimal digits by the appropriate power of 16:
146*160 = 146* 1 = 146
170*162 = 170*256 = 43,520
-------------------------------
43,666
Keywords
binary,hexadecimal,hex,color codes,boolean,base 2,base 16