Decimal versus binary
When we refer to a number such as 532 in base 10, what we actually mean is the number:
(5 * 10^2) + (3 * 10^1) + (2 * 10^0) = 532
Note that in each pair of parentheses,
the first number of the product is a single-digit number in the range [0, 9], and
the second number is a unique power of 10 (in sorted order from greatest to least).
We can do a similar calculation for numbers in other bases such as binary. For example,
0100 in binary (base 2) is equivalent to:
(0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 0 + 4 + 0 + 0 = 4 (in decimal)
Since we are working in base 2 now instead of base 10, all of the exponent bases are 2s
instead of 10s, and all of the first numbers in each parenthesized product is an integer
in the range [0, 1].
However, a number like 0100 might be interpreted as one-hundred in decimal. Thus, we
sometimes prefix binary numbers with "0b" (zero b) to differentiate binary numbers from
numbers in base 10 representation (so instead of using 0100, we would say
that 4 in decimal is equivalent to
0b0100
in binary).Now let's try a more complicated example: convert
0b10101001
to decimal. We will assume
this is unsigned for now (more on representing positive versus negative numbers later).0b10101001
= 1 * 2^7 + 0 * 2^6 + 1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0
= 2^7 + 2^5 + 2^3 + 2^0
= 128 + 32 + 8 + 1
= 169
What the hex is going on here?
Binary isn't the only base that is useful. Hexadecimal
is the base-16 number system, as opposed to the base 2 system (binary) and base 10 system
(decimal). The prefix we use for hexadecimal is "0x". To represent the numbers 0-9, we simply
use those digits. To represent 10-15, we use the letters A-F.
A | B | C | D | E | F |
10 | 11 | 12 | 13 | 14 | 15 |
Note: Hexadecimal is oftentimes referred to as "hex" for short. Do not confuse this with
the rarely-used base 6 representation heximal. That is, if you hear someone talking about
a "hex" number, they are almost certainly referring to a number in base 16 rather than a
number in base 6.
Calculating the decimal value of a hexadecimal number follows much the same pattern as binary.
For an example, we will convert
0xCA1
to decimal:0xCA1
= 12 * 16^2 + 10 * 16^1 + 1 * 16^0
= 3072 + 160 + 1
= 3233
As you can see, with only 3 digits in hex, we were able to represent a 4-digit number in
decimal, which is also a 12-bit number in binary. In fact, hexadecimal is a pretty
convenient way of shortening a binary string to make it more readable because each 4-bit
nibble of a binary string corresponds to a single digit in hex.
Let's take the binary representation
0b10101001
. Suppose we break this into 4-bit chunks,
0b1010
and 0b1001
. We can convert each 4-bit chunk into a hex value.0b1010 = 10 = 0xA
0b1001 = 9 = 0x9
Thus, we can compress the binary representation
0b10101001
into the hex representation
0xA9
. In much the same vein, you can "decompress" from hex to binary by converting each
hex digit into a 4-bit binary representation.Self-tests: Decimal, Binary, Hex
Convert the following to decimal:
0b1111 |
15 |
0xCA |
202 |
Convert the following to 8-bit binary numbers:
67 |
0b01000011 |
0xB1 |
0b10110001 |
Convert the following to hex:
0b01001011 |
0x4B |
255 |
0xFF |
What would be the length of the corresponding binary representation?
0xAF02E9D3
32-bits