COMP0068 Lecture4 Computer Arithmetic
COMP0068 Lecture4 Computer Arithmetic
COMP0068 Lecture4 Computer Arithmetic
Systems
(COMP0068)
Lecture 4: Computer Arithmetic
“two” = 102
Example
1011001012 = ?
6
Converting the other way ...
from decimal à binary ?
One way of converting from decimal to binary is by first
dividing by the “largest power of two” …
and then successively dividing by smaller ones
Repeated Division-by-2 Method
Quotient
(Result)
msb 1 0 1 lsb
1 0
22 ) 5 2 ) 1 2 ) 1
à 1012
Remainder
001010111110101110101101001000012
This has 32 bits (binary digits) – the standard size for
addresses and data in many current computers.
001010111110101110101101001000012 = 2BEBAD2116 10
Talking binary! (compared to talking decimal)
• HOWEVER ... this use of Kilo for 1024 rather than 1000 directly
conflicted with international S.I. unit nomenclature .... so ...
11
Introduction of kibibyte, mebibyte, etc.
• 1 kibibyte = 1KiB = 1024 bytes
• 1 mebibyte = 1MiB = 1024 KiB = 1,048,576 bytes
• 1 gibibyte = 1GiB = 1024 MiB, etc.
• BUT ... both nomenclatures are still in widespread use ... so often it
is the context which tells you whether it is a “binary” version of the
units or a “decimal” version of the units.
16
Decimal System - Subtraction
34510
27310 -
We all know how to subtract in decimal:
07210
Let digits of 345 be labeled: r2, r1, r0 r2=3, r1 =4, r0 =5
19
Negative numbers
Sign-magnitude notation for decimal numbers
In decimal, we simply use the +/- sign to indicate whether the number
is positive or negative.
-234
‘-’ is the sign and ‘234’ is the magnitude
-010102
• Note that the the remaining bits (excluding the sign bit) are interpreted differently
than for sign/magnitude numbers.
22
Converting from 2’s complement to
decimal
1. Invert all bits
2. Add ‘1’ to the LSB
3. The MSB is the sign bit
Example:
1001à 0110 à 0111 => +710
10012
01112 +
---------
1 0000
Note: When adding N-bit numbers, the carry out of the Nth bit is discarded.
23
Advantages of using the 2’s complement
and MSB in representing negative binary
numbers
24
2s Complement Form - Binary
Replace each symbol si in binary number with complement : 1- si
110100 +1 110101
…000…00000…00012310
QED! …999…99999…99987710 +
26
…000…00000…00000010
Complement Form - Hexadecimal
Replace each symbol si in binary number with complement : F- si
Example: 5FAB103416
A054EFCB16 +1 A054EFCC16
28
Questions to think about …
• Why didn’t computers not just use a ‘0’ or ‘1’ at the start of
numbers to indicate positive or negative numbers?
(i.e. a sign bit ... using sign & magnitude format ... similar
to what the LMC simulator does)
31