Last Time We studied representations of numbers and base numbering systems. We focused on three imporatant bases in the computing world, 2, 8, and 16.
Base numbering systems have an alpahet.
- {0,1} binary numbers, base 2
- {0,1,2,3,4,5,6,7} octal numbers, base 8
- {0,1,2,3,4,5,6,7,A,B,C,D,E,F} hexadecimal, base 16
We learned about money and the "greedy algorithm", which says, "Use the largest possible bill and use as many as you can. Keep doing this down through the denominations.
In base b, the "bills" are
1, b, b2, b3, ......
Here we convert 67 to binary from decimal.
denomination | number of "bills" | what's left |
---|---|---|
64 | 1 | 3 |
32 | 0 | 3 |
16 | 0 | 3 |
8 | 0 | 3 |
4 | 0 | 3 |
2 | 1 | 1 |
1 | 1 | 0 |
67 = 0b1000011
We also called this the bigendian algorithm, since it starts at the big end first.
We then considered the littleendian algorithm, which starts
at the little end. It is based on these two principles. If
b
is a base:
- If
x
is a number, you getx//b
by chopping off last digit - If
x
is a number, you get the last digit with x % b.
In this table, * stands for an unknown sequence of 0s and 1s. To get the second line, we compute 67//2 and note that the remainder is 1, so the binary expansion must end in a 1.
67 | * |
33 | *1 |
16 | *11 |
8 | *011 |
4 | *0011 |
2 | *00011 |
1 | *000011 |
0 | 1000011 |
Here we convert 67 to hex.
Take the binary expansion
1000011
Break it into groups of four from the little end, and translate each group into a single hex digit.
100 0011 4 3 -> 0x43convert back to binary 0x43 4 3 0100 0011 (now glue to get 001000011, then lop off leading 0s 0b1000011) 0x43 to decimal 4*16 + 3 = 67