Lecture 4 - The Digital Logic Level, Part 3
Lecture 4 - The Digital Logic Level, Part 3
Foundations of Computing
To Binary Numbers
Foundations of Computing: Technical Strand
João Filipe Ferreira
1
Numeral Systems
Foundations of Computing
The words ‘digital’ and ‘digit’ come from the latin
word ‘digitus’, which means finger
Foundations of Computing
the Ishango bones (30,000 BCE!)
European tally marks (w/ clustering)
tally marks – unary numeral system
counting 4
Numeral Systems
Roman Numerals
(from 900 BCE until Middle Ages)
ancient numbers
Foundations of Computing
counting 6
Numeral Systems
the evolution of the Indo-Arabic numeral system
Foundations of Computing
counting 7
Numeral Systems
the evolution of the Indo-Arabic numeral system
Foundations of Computing
counting 8
Numeral Systems
the evolution of the Indo-Arabic numeral system
Foundations of Computing
counting 9
Numeral Systems
the evolution of the Indo-Arabic numeral system
positional system
(i.e. it uses a place-value notation)
counting 10
Numeral Systems
positional system
(i.e. it uses a place-value notation)
bit= binary digit
162 161 160 16-1 16-2
A B C
counting numbers
Numeral Systems
• When we use numbers in every day life we write them using a
specific set of numbers (symbols) in a positional number
system:
• We use the decimal number system (base 10)
• Numbers range between 0 to 9
• Different positions n signify different weights (10n)
Foundations of Computing
• e.g. Take for instance: 12,687 1 2 6 8 7
Each position signifies a different
10000’s
1000’s
1’
10’
100’
s
s
weighting (each position is 10 times the
previous)
Position 5 4 3 2 1
Weight (base 10) 10000 1000 100 10 1
Decimal number 1 2 6 8 7
12
Weight
n 0
n * digit n (10000 *1) 1000 * 2 100 * 6 10 * 8 1* 7
Foundations of Computing
weight is double the preceding one:
• e.g. 10112
• Octal: - base 8 which means there are 8 different values before moving to the
next column – 0 to 7 (i.e. each position weight is 8x the previous);
• e.g. 318 (which is 2510 in decimal – note that in this particular situation, 13
the use of the radix is essential!)
Foundations of Computing
512 256 128 64 32 16 8 4 2 1
calculated by 2n
1 1 0 1 1 0 1 1 0 1
e.g. position weighting (2n)
20 = 1 º (1)
• In binary, this would be represented as: 1101101101
21 = 22 º (2)
• 22 =all
To work out what this is in decimal, we need to add 4 ºthe(2columns
x 2)
23 = 8 º (2 x 2 x 2)
with a “1” in them. 24 = 16 º (2 x 2 x 2 x2)
25 = 32 º (2 x 2 x 2 x 2 x 2)
Therefore,
14
Weight n * digitn (512 *1) (256 *1)(128 * 0) (64 *1) (32 *1) (16 * 0) 8 *1 4 *1 2 * 0 1*1 87710
n 0
11011011012 87710
Converting from Decimal
• There are two ways of converting a number
from decimal to binary.
• Method 1: Division Method E.g. Convert 15510 to binary
• Steps / algorithm: 15510 / 210 = 7710 77.5 77(r 1)
1. Divide the decimal number by 2 (i.e. the
Foundations of Computing
7710 / 210 = 3810 38.5 38(r 1)
number base you want to convert to).
3810 / 210 = 1910 19 19(r 0)
2. If there is a fraction left over (i.e. 0.5) then
set remainder bit (r) to 1, otherwise (i.e. no 1910 / 210 = 910 9.5 9(r 1)
fraction left over), set remainder bit (r) to 0. 910 / 210 = 410 4.5 4(r 1)
3. Set new decimal number to the lowest
410 / 210 = 210 2 2(r 0)
whole number from the division
4. If decimal number not equal to 0, repeat 210 / 210 = 110 1 1(r 0)
Foundations of Computing
128 64 32 16 8 4 2 1 172 – 128 = 44 (set r = 1)
44 – 64 = -ve number (set r = 0)
1 0 1 0 1 1 0 0
44 – 32 = 12 (set r = 1)
12 – 16 = -ve number (set r = 0)
17210 = 101011002 12 – 8 = 4 (set r = 1)
MSB LSB 4 – 4 = 0 (set r = 1)
(most significant bit) (least significant bit)
0 – 2 = -ve number (set r = 0)
0 – 1 = -ve number (set r = 0) 16
Foundations of Computing
• 10010 to binary
• 231710 to binary
• 58910 to binary
• Use the following grid to help you in your workings…
4096 2048 1024 512 256 128 64 32 16 8 4 2 1
17
Unsigned Binary Integers
• In the decimal system, the term unsigned denotes positive whole numbers
• Therefore, an unsigned binary number would also represent a positive
whole number
• The range of values are between (0 < x < 2 n -1) where n is the number of bits
• Some terminology:
Foundations of Computing
• bit = a single 1 or 0 binary digit
• bits = multiple 1 or 0 bits
• nibble = 4 bits
• Byte = 8 bits
• Word = number of bits a CPU can process in one go (e.g. Intel Core i7 = 64 bits)
Foundations of Computing
• For example: 8 bit numbers
• Number of bits, n = 8 128
-/+ 64 32 16 8 4 2 1
Foundations of Computing
• Multiplication
• However, we seem to be able to represent both
a positive and a negative 0:
• 1000 00002 = -010, 0000 00002 = +010
• Can we add/subtract with this oddity with 0?
• Answer: to perform these operations we need to be aware of
one’s and two’s complement 20
One’s Complement
• As unsigned numbers can be +ve or –ve,
using one’s complement removes the positive and negative 0
problem from binary signed integers
• Lets say we need to represent a negative number…
• In this case, we have to invert all of the binary digits:
Foundations of Computing
• + 1110 = 0000 10112
• - 1110 = 1111 01002
• However, this introduces a problem for addition:
• An overflow bit must be wrapped around as a carry bit
• Example: 1310 + (-210)
00001101
11111101+ 21
Overflow bit 100001010 Carry bit
1+
00001011 = 1110
Two’s Complement (i)
• Overflow issue in one’s complement is problematic
• It slows down addition
• It is complex to implement in hardware
• Two’s complement removes the need for the overflow carry
Foundations of Computing
• However we need to work with a fixed precision (e.g. 8 bits)
rather than taking shortcuts
Foundations of Computing
• Think of the weights as:
Position 8 7 6 5 4 3 2 1
Weight (base 10) -128 64 32 16 8 4 2 1
Foundations of Computing
• 12 + 02 = 110 = 012
1 1
• 12 + 12 = 210 = 102 1
0
0 1 0 1
1 +
0
0 1 0 0 +
0
1
• Example: 510 + 410 = 910 1 0 0 1 1
0
0000 0101
0000 0100+
0000 1001 24
Basic Binary Addition (ii)
Carry bit
• Handling overflow:
1
• Consider the carry example in the last slide 0 1 0 1
• 110 + 110 = 210 = 102 0 1 0 0
1 0 0 1
• What if those bits were 1’s? 1 1
Foundations of Computing
0 1 1 0 1 1
• We need 1 more bit (or overflow)
0 1 1 0 0 1 +
• 110 + 110 + 110 = 310 = 112
1 1 0 0 1 1
1
Carry bits
• ... and higher values? 1 0 1
0 0 1 0 0 0 1
(adding 3+ numbers at once) 1
• 110 + 110 + 110 + 110 = 410 = 1002 0 0 1 1 0 1 1
0 0 1 1 0 0 1 +
• Write the carry in one place at a time.
1 0 0 0 0 1 0 25
10
Two’s Complement Addition
• Uses the same ‘tricks’ as binary addition however must use a
fixed length.
• Example: -5310 + 7810 as 8 bit binary numbers
• +7810 = 0100 11102
Foundations of Computing
• +5310 = 0011 01012
• -5310 = 1100 10112 (invert the bits and add one)
Foundations of Computing
27
Binary Multiplication (i)
• Binary multiplication is:
• Multiple additions
• Shifting the number
10112 1110
Current bit is: 1
0
Foundations of Computing
11012 x 1310
Add the value: 11012 11012
Add the value: 11012 11010 2 Shift left one position
Foundations of Computing
Subtraction: 6410 - 3110
29
Summary
• In this lecture we have looked at:
• Decimal & Binary numeral systems
• One’s & Two’s complement
• Binary arithmetic:
• Addition
Foundations of Computing
• Subtraction
• Multiplication
Directed Study
1. Read Appendix A (Binary Numbers) & B (Floating Point
Numbers) - 6th Edition, Structured Computer Organisation.
Investigate
2. Floating point numbers. 30
3. Hexadecimal numbers.