Welcome Survey Due Today
Access via
Yan, SP25
02-Number Representation (1)
Number Representation
Great book ⇒� The Universal History of Numbers� by Georges Ifrah
Lecture PDF: link
CS61C
Great Ideas
in
Computer Architecture
(a.k.a. Machine Structures)
cs61c.org
Assistant
Teaching Professor
Lisa Yan
Yan, SP25
02-Number Representation (2)
Digital data not necessarily born Analog…
Yan, SP25
02-Number Representation (3)
Data input: Analog → Digital
Real world is analog!
To import analog information, we must do two things
Yan, SP25
02-Number Representation (4)
Every Base is Base 10…?
How many rocks?
Yan, SP25
02-Number Representation (5)
Binary, Decimal, Hex
Agenda
Yan, SP25
02-Number Representation (6)
Number vs Numeral
Numeral
A symbol or name that stands for a number
e.g., 4 , four , quatro , IV , IIII , …
…and Digits are symbols that make numerals
Above the abstraction line
Below the abstraction line
Number
The “idea” in our minds…there is only ONE of these
e.g., the concept of “4”
Abstraction Line
Yan, SP25
02-Number Representation (7)
Decimal: Base 10 (Ten) #s
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Example:
3271 = 327110 = (3x103) + (2x102) + (7x101) + (1x100)
Yan, SP25
02-Number Representation (8)
Base 2 (Two) #s, Binary
Digits: 0, 1 (binary digits -> bits)�
Example: Binary number “1101”
Convert to decimal:
0b1101 = 11012 = (1x23) + (1x22) + (0x21) + (1x20)
= 8 + 4 + 0 + 1
= 13
Common binary shorthand: 0b1101
Yan, SP25
02-Number Representation (9)
Base 16 (Sixteen) #s, Hexadecimal
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
10, 11, 12, 13, 14, 15
Example: Hexadecimal number “A5”
Convert to decimal:
0xA5 = A516 = (10x161) + (5x160)
= 160 + 5
= 165
“Hex” for short. Common hex shorthand: 0xA5
Yan, SP25
02-Number Representation (10)
Convert from Decimal to Binary
E.g., 13 to binary?
Start with the columns
Left to right, is (column) ≤ number n?
23 = 8 | 22 = 4 | 21 = 2 | 20 = 1 |
| | | |
13
5
1
1
1
0
1
0
0b1101
Yan, SP25
02-Number Representation (11)
Convert from Decimal to Hexadecimal
E.g., 165 to hexadecimal?
Start with the columns
Left to right, is (column) ≤ number n?
163 = 4096 | 162 = 256 | 161 = 16 | 160 = 1 |
| | | |
0
0
10(A)
5
0x00A5
165
5
0
Yan, SP25
02-Number Representation (12)
Nibbles and Bytes
Memorize this table.
Dec | Hex | Bin |
00 | 0 | 0000 |
01 | 1 | 0001 |
02 | 2 | 0010 |
03 | 3 | 0011 |
04 | 4 | 0100 |
05 | 5 | 0101 |
06 | 6 | 0110 |
07 | 7 | 0111 |
08 | 8 | 1000 |
09 | 9 | 1001 |
10 | A | 1010 |
11 | B | 1011 |
12 | C | 1100 |
13 | D | 1101 |
14 | E | 1110 |
15 | F | 1111 |
Yan, SP25
02-Number Representation (13)
Convert Binary -> Hexadecimal
Dec | Hex | Bin |
00 | 0 | 0000 |
01 | 1 | 0001 |
02 | 2 | 0010 |
03 | 3 | 0011 |
04 | 4 | 0100 |
05 | 5 | 0101 |
06 | 6 | 0110 |
07 | 7 | 0111 |
08 | 8 | 1000 |
09 | 9 | 1001 |
10 | A | 1010 |
11 | B | 1011 |
12 | C | 1100 |
13 | D | 1101 |
14 | E | 1110 |
15 | F | 1111 |
Memorize this table.
Yan, SP25
02-Number Representation (14)
Which base do we use?
Yan, SP25
02-Number Representation (15)
The computer knows it, too…
#include <stdio.h>
int main() {
const int N = 1234;
printf("Decimal: %d\n",N);
printf("Hex: %x\n",N);
printf("Octal: %o\n",N);
printf("Literals (not supported by all compilers):\n");
printf("0x4d2 = %d (hex)\n", 0x4d2);
printf("0b10011010010 = %d (binary)\n", 0b10011010010);
printf("02322 = %d (octal, prefix 0 - zero)\n", 0x4d2);
return 0;
}
Output Decimal: 1234
Hex: 4d2
Octal: 2322
Literals (not supported by all compilers):
0x4d2 = 1234 (hex)
0b10011010010 = 1234 (binary)
02322 = 1234 (octal, prefix 0 - zero)
(more next time)
Yan, SP25
02-Number Representation (16)
Pop quiz??
1011
A. 4 C. 16
B. 8 D. 64
E. Something else
A. 1
B. 9 (π=3.14, so 0.011”.” 001100)
C. 64 (Macs are 64-bit machines)
D. Every bit the machine has
E. ∞
Yan, SP25
02-Number Representation (17)
Yan, SP25
02-Number Representation (18)
Pop quiz??
1011
A. 4 C. 16
B. 8 D. 64
E. Something else
A. 1
B. 9 (π=3.14, so 0.011”.” 001100)
C. 64 (Macs are 64-bit machines)
D. Every bit the machine has
E. ∞
=2^4
Yan, SP25
02-Number Representation (19)
BIG IDEA: Bits can represent anything!!
With N bits, you can represent at most 2^N things.
Green (B5)
Blue (15)
Red (FD)
California Gold
FDB515
Yan, SP25
02-Number Representation (20)
Integer Representations
Agenda
Yan, SP25
02-Number Representation (21)
How do we pick a representation for integers?
1010
+ 0111
10001
11
carry bits
Yan, SP25
02-Number Representation (22)
What if “too big”? Overflow
Integer overflow: The arithmetic result is outside the representable range.
…00000001010
0000
1111
0001
1110
…
Yan, SP25
02-Number Representation (23)
Many Possible Number Representations
0…0
1…1
0…01
1…10
…
0
1
2N-2
2N-1
Yan, SP25
02-Number Representation (24)
Sign-Magnitude, Ones’ Complement
Agenda
Yan, SP25
02-Number Representation (25)
Sign-Magnitude: Ain’t no free lunch
0x00000000 = +0ten
0x80000000 = –0ten
0b 1000 0000 … 0000
…
…
1001
0001
0000
0111
1111
1000
Yan, SP25
02-Number Representation (26)
Ones’ Complement: Another try
0b 1111 1000
-7ten =
+7ten = 0b 0000 0111
…
…
1110
0001
0000
0111
1000
1111
Yan, SP25
02-Number Representation (27)
Shortcomings of Ones’ Complement?
…
…
1110
0001
0000
0111
1000
1111
Yan, SP25
02-Number Representation (28)
Two’s Complement
Agenda
Yan, SP25
02-Number Representation (29)
Two’s Complement: C23 standard number rep.
…
…
1110
0001
0000
0111
1000
1111
…
0001
0000
0111
1000
…
1110
1111
7
-8
…
-2
-1
0
1
…
Yan, SP25
02-Number Representation (30)
Arithmetic in Two’s Complement is simple
5
+ -5
0
0101
+ 1011
0000
Two’s complement
Decimal
…
0001
0000
0111
1000
…
1110
1111
7
-8
…
-2
-1
0
1
…
Yan, SP25
02-Number Representation (31)
Arithmetic in Two’s Complement is simple
5
+ -5
0
0101
+ 1011
0000
Two’s complement
Decimal
…
0001
0000
0111
1000
…
1110
1111
7
-8
…
-2
-1
0
1
…
111
carry bits
1
dropped
Yan, SP25
02-Number Representation (32)
Two’s Complement: Formula
0b0101
= (0 x –23) + (1 x 22) + (0 x 21) + (1 x 20)
= 0 + 4 + 0 + 1
= 5
0b1011
= (1 x –23) + (0 x 22) + (1 x 21) + (1 x 20)
= -8 + 0 + 2 + 1
= -5
Yan, SP25
02-Number Representation (33)
Two’s Complement: Algorithm
0b0101
= (0 x –23) + (1 x 22) + (0 x 21) + (1 x 20)
= 0 + 4 + 0 + 1
= 5
0b1011
= (1 x –23) + (0 x 22) + (1 x 21) + (1 x 20)
= -8 + 0 + 2 + 1
= -5
0101 → 1010 → 1011
5ten
-5ten
1011 → 0100 → 0101
-5ten
5ten
At home: Prove algorithm is equivalent to formula!
Yan, SP25
02-Number Representation (34)
Two’s Complement: C standard (as of 2024)
…
0001
0000
0111
1000
…
1110
1111
[READ AT HOME]
Yan, SP25
02-Number Representation (35)
Two’s Complement: Integer Overflow
…
0001
0000
0111
1000
…
1110
1111
0000
0001
1000
0111
1001
1111
1110
0010
…
…
0
1
2
7
-7
-8
-2
-1
⚠️
⚠️
⚠️
[READ AT HOME]
Yan, SP25
02-Number Representation (36)
And in summary…
[READ AT HOME]
Yan, SP25
02-Number Representation (37)
Bias Encoding
[READ AT HOME]
Agenda
Yan, SP25
02-Number Representation (38)
Bias Encoding
39
0
0
[READ AT HOME]
Yan, SP25
02-Number Representation (39)
Bias Encoding: N = 4, bias = -7
0001
1000
0111
1001
1111
1110
0010
…
…
-7
-6
-5
0
2
1
7
8
0000
Example: N = 4, bias = -7
[READ AT HOME]
Update 1/27; see Ed
Yan, SP25
02-Number Representation (40)
Yan, SP25
02-Number Representation (41)
Yan, SP25
02-Number Representation (42)
Yan, SP25
02-Number Representation (43)