Base 10 (Decimal)
We live in a decimal world, meaning we live in a base 10 world. In our number system, there are ten digits, 0-9. Once we reach the maximum value (9) in a column, we roll it over to the start (0), and increment the next column to the left.
_8, _9, 10, 11,...,18, 19, 20, ...
The decimal number system, like all number systems, has place values. Place values are the standard unit value for each column in a number. Place value increase from right to left. The place values are determined by the powers of the base, starting at 0. The number 123,456 can be represented with place values as follows:
1 | 2 | 3 | 4 | 5 | 6 |
100000 | 10000 | 1000 | 100 | 10 | 1 |
105 | 104 | 103 | 102 | 101 | 100 |
Base 16 (Hexadecimal)
Hexadecimal, or hex, has 16 digits, 0-15. However, 10-15 are not single digits, so letters are used in their place (A=10, B=11, C=12, D=13, E=14, F=15). Since 16 is a power of two, hexadecimal is often used in computers in a variety of ways. It can become a shorthand for storing bytes of information. Since it takes 4 bits to represent the numbers from 0-15, any byte of information can be represented with only two hex values.
Base 16 follows all of the same rules as base 10: place values start with a power of 1 and increase from left to right. The only differences are:
- The base used in the columns is 16
- There are 16 digits, 0-F.
So, the columns of the hex counting system start as:
164 | 163 | 162 | 161 | 160 |
65536 | 4096 | 256 | 16 | 1 |
An example of a hexideciaml number is A5F4
16.
Converting Hex to Decimal
To calculate the value of a decimal value using place values we multiply the digit in the column by the value of the column, so 123456
10 evaluates to
1x100000 + 2x10000 + 3x1000 + 4x100 + 5x10 + 6x1 = 100000 + 20000 + 3000 + 400 + 50 + 6 = 123,456
Converting hex to decimal follows the same process. We take the digit in the column and multiply by the value of the column. Then, we add each value together. So, given A5F4
16, we see
A | 5 | F | 4 |
163 | 162 | 161 | 160 |
4096 | 256 | 16 | 1 |
A5F4 = 10x4096 + 5x256 + 15*16 + 4x1 = 40960 + 1280 + 240 + 4 = 42484
Converting Decimal to Hex
If I want to break a base 10 number up into place values, I find the largest place value that goes into the number. For example, given the number 4562, the largest place value that goes into 4562 is 1000. It goes in 4 times, so we place a 4 in the 1000s columns.
This leaves a remainder of 562. The next highest column that goes in is 100, going in 5 times and leaving a remainder of 62.
This process continues until we run out of columns.
Again, we use the same process to move from decimal to hex. When working with the value 343
10, we start with the highest hex place value that is less than 343. The hex columns are 1, 16, 256, 4096,... So, we start with the 256 column, also known as 16
2. So, we divide 347 by 256 for a value of 1 with a remainder of 91.
Next, we check the next highest column value. It happens to be 16, which is less than our remainder of 91. So, we devide 91 by 16 and get 5 with a remainder of 11.
Since there less than 15 left, we place that value in the 1s column. But, since the value is 11, we translate that to the digit B.
So, our final value is 15B
16.
Converting Between Binary and Hex
Common sense tells us that to go between binary and hex, we have to go through base 10. This can be a time consuming and difficult process, prone to mistakes. However, since a single hex digit is 4 binary digits and two hex digits (1s and 16s) can give us the numbers from 0-255, there is an easier way. The shortcut is to convert each hex digit to 4 bits, individually. So, given the number 4E3
16, we can find the binary equivalant by converting each hex digit to its binary equivalent.
4 => 0100
E => 1110
3 => 0011
So, the binary equivalent is 010011100011. But, we can leave off the leading zero to get 10011100011
2.
Going the other way (binary to hex) is just as easy. We simply group the binary digits into groups of 4. CAUTION: Since leading zeros can be left off, be sure to form your groups from right to left. For example, if we start with 1100110011, we break the bits up as 11 | 0011 | 0011 and then add leading zeros to the first to make it a byte 0011 | 0011 | 0011. Now, translate each 4 bits into the hex equivalent:
0011 => 3
0011 => 3
0011 => 3
So the hex equivalent is 333
16.
Real World Applications
A pixel is the smalled dot that can be viewed on a computer monitor. Monitors are made of thousands of pixels to produce the images that are seen on the screen. Each pixel has three lights: red, green and blue (RGB color). Each light uses a 1 byte value (0-255) to represent its state, where 0 if off and 255 is full on. Since each of the three lights has a possible 256 values, that provides us with 16,777,216 color choices, or millions of colors. It would be very cumbersome for programmers to write each of those colors with bits as each color used would require 24 bits of information (24-bit color). So, programmers often use shorthand notation for the colors, substituting hex values for each bit of color. This allows each color to be represented by 6 hex digits: 2 for red, 2 for green and 2 for blue. So, if we want the red full on, the green at 100 and the blue off, we translate each of those decimal values to hex:
255 => FF
100 => 64
0 => 00
So the hex color is #FF6400.