Show Lecture.Bases as a slide show.
CS253 Bases
Bases
- We’re used to base 10, because
we have that many toes.
Really.
- There’s nothing intrinsically good or bad about base 10.
- Vulcans use
base 12. Well, in my head-canon, they do.
- In base N, the digits are 0…N−1.
- Base 8 has no digit 8,
just as base 10 has no digit ⑩.
- Digits after 9 are usually letters: A…Z or a…z.
Writing integers (mathematics)
- When writing numbers in mathematics, they are by default in base 10.
- A subscript indicates an explicit base:
278 + 436 = 3216
Writing integers (C++ source code)
-
0b…
or 0B…
- It’s binary, base 2.
-
0x…
or 0x…
- It’s hexadecimal (“hex”),
base 16. “Hexa” is Greek, “decimal” is Latin. 😼
-
0
digits - It’s octal (base 8).
Plain
0
is octal, not decimal!
It’s 0⋅8⁰, not 0⋅10⁰.
- Otherwise
- It’s decimal (base 10).
“Decimal” ≠ “digits after the decimal point” here.
Write for readability: sizes in decimal, bitmasks in binary, octal, or hex.
cerr << 0b11 << ' ' << 0XfF << ' ' << 012 << ' ' << 34;
3 255 10 34
Place notation
Consider an ordinary, three-digit base 10 number.
It has a hundreds place, a tens place, and a ones place.
You know this so well that you’ve forgotten it.
So, 78910 means:
- 7⋅102 + 8⋅101 + 9⋅100
- 7⋅100 + 8⋅10 + 9⋅1
- 700 + 80 + 9
- 789
Place notation
Now, consider a base 8 number, such as 3758.
This means:
- 3⋅82 + 7⋅81 + 5⋅80
- 3⋅64 + 7⋅8 + 5⋅1
- 192 + 56 + 5
- 253
Place notation
A binary number, such as 1 01102, means:
- 1⋅24 + 0⋅23 + 1⋅22 + 1⋅21
+ 0⋅20
- 1⋅16 + 0⋅8 + 1⋅4 + 1⋅2 + 0⋅1
- 16 + 0 + 4 + 2 + 0
- 22
Place notation
Of course, in practice, we wouldn’t bother writing down
the terms with zero coefficients, since they contribute only
zeroes to the sum.
A binary number, such as 1 01102, means:
- 1⋅24 + 1⋅22 + 1⋅21
- 1⋅16 + 1⋅4 + 1⋅2
- 16 + 4 + 2
- 22
I’m thinking of a number…
- First we need a number for examples.
- How about 0644?
- The 0 in the beginning just means it is in octal, in C++.
- What is it though?
- In C++,
0644
is an int, just as 42
is.
0644 in octal… okayyyyyy…
- What is it in different bases?
- So, first what does 644 mean in octal?
- 6 in the 64’s place (64 = 8²)
- 4 in the 8’s place (8 = 8¹)
- 4 in the 1’s place (1 = 80)
What is 0644 in decimal?
- Octal to decimal
- Not hard, we just need to do the math.
- It’s just arithmetic.
- 6448 = 6⋅8² + 4⋅8¹ + 4⋅80
- 6448 = 6⋅64 + 4⋅8 + 4⋅1
- 6448 = 384 + 32 + 4
- 6448 = 420
What is 0644 in binary?
Hardest way is convert to decimal, then convert to binary.
- Hard way: Translate to decimal, then to octal.
- 6⋅8² = 384
- 4⋅8¹ = 32
- 4⋅80 = 4
- Total = 420, or 256+128+32+4, or 1 1010 01002
- Easy Way: Break it into pieces of 3 each.
- Why 3? 8¹ = 23, that’s why!
- One digit in octal is three digits in binary!
- 68 = 1102 and 48 = 1002,
so 06448 = 110 100 1002
What is 0644 in hexadecimal?
Decimal | Hex |
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
- Hard way: Translate to decimal (420), then to hex.
- Hexadecimal is base 16, so the places are 4096, 256, 16, 1
(163, 16², 16¹, 160).
- How many of each?
- 420 = 1⋅256 + 10⋅16 + 4⋅1
- So, it’s 1a416
What is 0644 in hexadecimal?
- Easy way:
- We already know it is 1 1010 0100 in binary.
- 16¹ = 24
- Each hex digit is 4 binary digits.
- 1 1010 0100
- 12 is easy: 1
- 10102 is 8+2 = 10 = a
- 01002 is 4 … so 4
- 1a416
Why doesn’t the easy way always work?
It’s all about the ratios:
From | To | Ratio | Hard? |
Octal | Binary | 8 = 23 | Easy |
Hexadecimal | Binary | 16 = 24 | Easy |
Hexadecimal | Octal | 16 = 8log₈16 = 84/3 | OK |
Hexadecimal | Decimal | 16 = 10log₁₀16 = 101.20412 | Hard |
Decimal | Octal | 10 = 8log₈10 = 81.1073 | Hard |
Decimal | Binary | 10 = 2log₂10 = 23.3219 | Hard |
Everything’s simple, unless stupid decimal (🖐🖑)
is involved.
So, our number is:
1 1010 01002 or 6448 or 42010
or 1a416
- What’s the difference?!
- To a human? It’s the difference between a number and a headache.
- To the computer? Nothing!
The computer doesn’t care!
cout << 0b10000 << ' ' << 020 << ' ' << 16 << ' ' << 0x10 << '\n';
16 16 16 16
- It stores it all in binary.
- They all look the same in binary.
- They’re all just ints.
- In fact, even if it DID care, it can’t tell the difference anyway!
- If a memory cell contains 0001 1011 0000 1011, was it binary,
octal, decimal, or hex to begin with?
How does it know?
It doesn’t know—you have to tell it!
int x = 0b10101;
cout << x << ' ' << x << '\n'
<< oct << x << ' ' << x << '\n'
<< dec << x << ' ' << x << '\n'
<< hex << x << ' ' << x << '\n';
21 21
25 25
21 21
15 15
- Alas, there is no
bin
.
- I/O manipulators such as hex and oct are “sticky”;
they persist until changed.