DSA2102 Essential Data Analytics Tools: Numerical Computation
Lecture 2: Binary Numbers
Tim Wertz
Department of Mathematics
National University of Singapore
Wertz DSA2102 Lecture 2 1 / 13
Numeral Systems
Decimal
833.71 = 8 · 102 + 3 · 101 + 3 · 100 + 7 · 10−1 + 1 · 10−2
Binary
1010.01 = 1 · 23 + 0 · 22 + 1 · 21 + 0 · 20 + 0 · 2−1 + 1 · 2−2
Hexadecimal
3F01.A2 = 3 · 163 + 15 · 162 + 0 · 161 + 1 · 160 + 10 · 16−1 + 2 · 16−2
NOTE: we often use “decimal” when we actually mean “decimal
point.” In this context, though, “decimal” means “base 10.”
Wertz DSA2102 Lecture 2 2 / 13
Numeral Systems
Only the single digits 0 and 1 are unambiguous
10 = ten or 10 = two?
If b is an integer ≥ 2, then b 0 = 1 and b 1 = b
Then, in base b, we have that b would be represented as 10. That is,
(b)b = 10
Why do we use different bases?
We use base 10 because most modern humans find it the most natural
We use base 2 because it is the most natural for computers
We use base 16 because it is more compact than base 2 but is easily
translatable
(165)10 = (10100101)2 = (A5)16
(15)10 = (1111)2 = (F)16
Wertz DSA2102 Lecture 2 3 / 13
Computer Representation
Bits and bytes □ □■■■□□■□
|{z} | {z }
1 bit 1 byte =8 bits
Integers
(88)10 = (1011000)2 = |{z}
□ ■□■■□□□
| {z }
sign magnitude
(−88)10 = (1011000)2 = |{z}
■ ■□■■□□□
| {z }
sign magnitude
Fixed-point numbers
(12.875)10 = (1100.111)2 = |{z}
□ ■■□□
| {z } ■■■
| {z }
sign integer fraction
7 103 1100111
12 = = = (1100.111)2
8 10 8 10 1000 2
Wertz DSA2102 Lecture 2 4 / 13
Binary numbers
Computers use binary numbers instead of decimal numbers.
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
10 = (ten)
First few natural numbers: 0, 1, 2, 3, 4, 5, 6
The sequence am am−1 · · · a1 a0 means
am × (ten)m + am−1 × (ten)m−1 + · · · + a1 × (ten) + a0
Digits: 0, 1
10 = (two)
First few natural numbers: 0, 1, 10, 11, 100, 101, 110
The sequence am am−1 · · · a1 a0 means
am × (two)m + am−1 × (two)m−1 + · · · + a1 × (two) + a0
Wertz DSA2102 Lecture 2 5 / 13
Binary numbers
Decimal numbers with a fractional part:
am am−1 · · · a1 a0 .b1 b2 · · · bn
= am × (ten)m + am−1 × (ten)m−1 + · · · + a1 × (ten) + a0
+ b1 × (ten)−1 + b2 × (ten)−2 + · · · + bn × (ten)−n
Binary numbers with a fractional part:
am am−1 · · · a1 a0 .b1 b2 · · · bn
= am × (two)m + am−1 × (two)m−1 + · · · + a1 × (two) + a0
+ b1 × (two)−1 + b2 × (two)−2 + · · · + bn × (two)−n
For clarity, we use (am am−1 · · · a1 a0 .b1 b2 · · · bn )10 to denote decimal
numbers and (am am−1 · · · a1 a0 .b1 b2 · · · bn )2 to denote binary numbers.
Wertz DSA2102 Lecture 2 6 / 13
Examples of binary numbers
Example 1:
(10101)2 = (1 × 24 + 0 × 23 + 1 × 22 + 0 × 21 + 1)10
= (1 × 24 + 1 × 22 + 1)10
= (16 + 4 + 1)10 = (21)10
Example 2:
(1.0101)2 = (1 + 1 × 2−2 + 1 × 2−4 )10 = (1.3125)10
Example 3:
(101.101)2 = (22 + 1 + 2−1 + 2−3 )10 = (5.625)10
Wertz DSA2102 Lecture 2 7 / 13
Additions
(0)2 + (0)2 = (0)2 , (0)2 + (1)2 = (1)2
(1)2 + (0)2 = (1)2 , (1)2 + (1)2 = (10)2 (1)2 + (1)2 + (1)2 = (11)2
(11011)2 + (10111)2 = ?
11011
+ 10111
11111
110010
Verification:
(11011)2 = (24 + 23 + 21 + 1)10 = (27)10
(10111)2 = (24 + 22 + 21 + 1)10 = (23)10
(110010)2 = (25 + 24 + 21 )10 = (50)10
Wertz DSA2102 Lecture 2 8 / 13
Additions
(0)2 + (0)2 = (0)2 , (0)2 + (1)2 = (1)2
(1)2 + (0)2 = (1)2 , (1)2 + (1)2 = (10)2 (1)2 + (1)2 + (1)2 = (11)2
(10.011)2 + (1.0111)2 = ?
10.011
+ 1.0111
11
11.1101
Verification:
(10.011)2 = (21 + 2−2 + 2−3 )10 = (2.375)10
(1.0111)2 = (1 + 2−2 + 2−3 + 2−4 )10 = (1.4375)10
(11.1101)2 = (21 + 1 + 2−1 + 2−2 + 2−4 )10 = (3.8125)10
Wertz DSA2102 Lecture 2 9 / 13
Subtractions
(0)2 − (0)2 = (0)2 , (1)2 − (0)2 = (1)2
(10)2 − (1)2 = (1)2 , (10)2 − (1)2 − (1)2 = (0)2
(11)2 − (1)2 − (1)2 = (1)2
(11010)2 − (10111)2 = ?
11010
− 10111
111
00011
Verification:
(11010)2 = (24 + 23 + 21 )10 = (26)10
(10111)2 = (24 + 22 + 21 + 1)10 = (23)10
(11)2 = (21 + 1)10 = (3)10
Wertz DSA2102 Lecture 2 10 / 13
Subtractions
(0)2 − (0)2 = (0)2 , (1)2 − (0)2 = (1)2
(10)2 − (1)2 = (1)2 , (10)2 − (1)2 − (1)2 = (0)2
(11)2 − (1)2 − (1)2 = (1)2
(10.011)2 − (1.0111)2 = ?
10.011
− 1.0111
11 111
00.1111
Verification:
(10.011)2 = (21 + 2−2 + 2−3 )10 = (2.375)10
(1.0111)2 = (1 + 2−2 + 2−3 + 2−4 )10 = (1.4375)10
(0.1111)2 = (2−1 + 2−2 + 2−3 + 2−4 )10 = (0.9375)10
Wertz DSA2102 Lecture 2 11 / 13
Multiplications
(1.0111)2 × (10.11)2 = ?
We first ignore the decimal points and multiply as integers
10111
× 1011
10111
10111
10111
11111101
There should be six bits to the right of the decimal point
(1.0111)2 × (10.11)2 = (11.111101)2
(1.0111)2 = (1.4375)10 , (10.11)2 = (2.75)10 ,
(11.111101)2 = (3.953125)10
Wertz DSA2102 Lecture 2 12 / 13
Summary
Numeral systems
Why computers use binary
Binary arithmetic
Wertz DSA2102 Lecture 2 13 / 13