ECE 223
Digital Circuits and Systems
Number System
M. Sachdev,
Dept. of Electrical & Computer Engineering
University of Waterloo
General Radix Representation
A decimal number such as 7392
represents
7392 = 7x103 + 3x102 + 9x101 + 2x100
It is practical to write only coefficients
and deduce power of 10s from position
In general, any radix (base) can be used
Define coefficients ai in radix r
0 <= ai <r
anrn + an-1rn-1 . a0r0 + .. a-mr-m
Common radics r = 2, 4, 8, 10, 16
2
General Radix Representation
r = 10 (Dec.)
r = 2 (Binary)
r = 8 (Octal)
r = 16 (Hex)
00
0000
00
01
0001
01
02
0010
02
03
0011
03
04
0100
04
05
0101
05
06
0110
06
07
0111
07
L7
08
1000
10
09
1001
11
10
1010
12
11
1011
13
12
1100
14
General Radix Representation
r = 10 (Dec.)
r = 2 (Binary)
r = 8 (Octal)
r = 16 (Hex)
13
1101
15
14
1110
16
15
1111
17
Usually, radix is shown as subscript
(1234.4)5 = 1x53 + 2x52 + 3x51 4x50 + 4x5-1 =
(513.4)10
(F75C.B)16 = 15x163 + 7x162 + 5x161 + 12x160 +
11x16-1 = (63,324.6875)10
Radix Conversion
The integral part of a decimal number to radix r,
repeatedly divide by r with reminders becoming ai
Convert
(77)10 to binary
Integer
Remainder
Coefficient
38
a0
19
a1
a2
a3
a4
a5
a6
77
(77)10 = (1001101)2
Examples
Convert (173)10 to r = 7
Integer
Remainder
Coefficient
24
a0
a1
a2
173
(173)10 = (335)2
Converting from Binary to decimal
(101101)2 = 1x25 + 0x24 + 1x23 + 1x22 +0x21 +1x20 =
(45)10
Fraction Conversion
To convert the fractional part of a number to radix r,
repeatedly multiply by r; integral parts of products
becoming ai
Convert (0.7215)10 to binary
0.7215x2
= 1.443
a-1 = 1
0.443x2
= 0.866
a-2 = 0
0.866x2
= 1.772
a-3 =1
0.772x2
= 1.544
a-4 = 1
0.544
= 1.088
a-5 = 1
(0.7215)10 = (0.10111..)2
Conversion between Binary/Octal/Hex
Nice simple ways to convert between these three number
systems, since all are a power of 2
Binary to Octal simply requires grouping bits into groups of
3-bits and converting
Binary to Hex simply requires grouping bits into groups of
4-bits and converting
Going the other direction (Octal to Binary or Hex to Binary)
should follow
Example (100111100101)2 = (4745)8 = (9E5)16
8
Number Systems- Book Sections
Number representations and conversions
are covered in Chapter 1, Sections 1.1
through 1.4
Complements
In computers, the representation and
manipulation of ve numbers is often
performed using complements
Complements for a radix come in two forms
Rs complement (Radix complement)
(r-1)s complement (Diminished radix complement)
For binary system -- 2s complement, & 1s
complement
10
rs Complement
Given a +ve number N with n digits
N = (an-1an-2 ..a0)
rs complement is defined as
rn N for N 0; zero otherwise
Examples 10s complement
(37218)10 = 105 37218 = 62782
(0.12345)10 = 100 0.12345 = 0.87655
Examples 2s complement
(101110)2 = 26 101110 = 010010
(0.0110)2 = 20 0.0110 = 0.1010
11
(r-1)s Complement
Given a +ve number N with n digit integer part & m
digit fractional part
N = (an-1an-2 ..a0..a-1a-2 .a-m)
(r-1)s complement is defined as
rn r-m - N
Examples 9s complement
(37218)10 = 105 1 - 37218 = 62781
(0.12345)10 = 100 10-5 - 0.12345 = 0.87654
Examples 1s complement
(101110)2 = 26 20 - 101110 = 111111 -101100 = 010011
(0.0110)2 = 20 2-4 - 0.0110 = 0.1111 0.0110 = 0.1001
12
Interesting Facts
The complement of a complement returns the
original number
Since we work with binary numbers a lot in
digital systems, it is really worth nothing that:
The
1s complement of a number is obtained by
flipping bits
The 2s complement of a number is obtained by
flipping bits and adding 1
13
Arithmetic Operations
Arithmetic operations - addition, subtraction,
multiplication can be performed in any radix
Example, r = 2
14
Addition (Unsigned Numbers)
Binary addition of unsigned numbers is done
just like in decimal. Add digits and generate
carries
We can get a non-zero
carry out; if we are limited to n-bits,
this becomes an overflow condition
Important:
15
Subtraction (Unsigned Numbers)
Binary subtraction is also similar to decimal
subtraction. Use borrow when needed
Doesnt seems to work if
second term is smaller!! A negative
result is often considered an underflow
Important:
16
Subtraction (Unsigned Numbers) Using
rs Complement
Direct method (using borrows) is fine if done
by hand, but a hassle in a digital system
Usage
of complement makes subtraction easier
to implement in hardware
The subtraction of two numbers (A-B)r can be
done as:
Add
rs complement of B to A
Y = A + (rn B) = (A B) + rn
17
Subtraction
If A B then carry out (rn) occurs; ignore it,
the rest is the subtracted result
If A < B
Y = A + (rn B) = rn - (A B)
Note: (A B) is a -ve number! Hence, take rs
complement and treat it as ve number
= rn Y = rn {rn - (A B)} = (A - B)
18
Subtraction Examples
10s complement subtraction
A = 72532, A = 27468
B = 03250, B = 96750
A B = 72532
B A = 03250
+ 96750
+ 27468
1 69282
30718
- 69282
19
Subtraction Examples
2s complement subtraction
A = 1010100, A = 0101100
B = 1000100, B = 0111100
AB=
1010100
BA=
+ 0111100
+
1 0010000
1000100
0101100
1110000
- 0010000
20
10
Subtraction (Unsigned Numbers) Using
(r-1)s Complement
Similar to rs complement subtraction
If A> B
Y = A + {(rn r-m) B} = (A B) + rn r-m
If end carry (rn) is discarded & least significant
carry (r-m) is added, result is the subtracted value
If A = B
Y = A + {(rn r-m) B} = (A B) + rn r-m =
0 + rn r-m
21
Subtraction (Unsigned Numbers) Using
(r-1)s Complement
If A < B
Y = A + {(rn r-m) B} = - (B A) + rn r-m
= rn r-m - (B A)
We take (r-1)s complement & treat it as a
ve number
rn r-m Y = rn r-m {rn r-m - (B A)} = (A - B)
22
11
Subtraction Examples
9s complement subtraction
A = 72532, A = 27467
B = 03250, B = 96749
A B = 72532
B A = 03250
+ 96749
+ 27467
1 69281
30717
+ 00001
69282
- 69282
23
Subtraction Examples
1s complement subtraction
A = 1010100, A = 0101011
B = 1000100, B = 0111011
AB=
1010100
BA=
+ 0111011
+
1 0010000
0000001
0010000
1000100
0101011
1101111
- 0010000
24
12
Signed Numbers
Computers handle signed numbers as well
Often required to represent both +ve & -ve numbers
in the same n-bit format
Left most bit (Most Significant Bit) represents the sign
0 +ve; 1 -ve
Three common representations
Numbers are represented in a fixed # of bits
Sign & magnitude
Signed 1s complement
Signed 2s complement
For +ve numbers, all three have same representation
25
Signed Numbers
Sign & magnitude
MSB is the sign bit; rest is the magnitude
For 8 bit word
+ 9 0000 1001; -9 1000 1001
Signed 1s complement
MSB is the sign bit
For +ve number actual value
For ve number 1s complement
+ 9 0000 1001; -9 1111 0110
Has 2 representations for 0; not widely used
26
13
Signed Numbers
Signed 2s complement
Common method of representing signed #s
Restrict the number range to (n-1) bits; Use 2s
complement for ve number representation
+ 9 0000 1001;
-9 1111 0111
With 5 bit word size, in signed 2s
complement we can represent number from
-16 to +15
Y = (-an-1)2n-1 + an-22n-2 + .. a020
Exercise write all number from -16 to +15
27
Addition of Signed Binary Numbers
2s complement
Perform the addition, ignore the carry
28
14
Subtraction of Signed Binary Numbers
2s complement
Take 2s complement of the # to be subtracted
(+/-A) (+/-B) = (+/-A) + (-/+B)
A, B are in signed 2s complement
Example, (-6) (-13) = +7
(1111 1010) (1111 0011)
1111 1010 + 0000 1101
1 0000 0111
=
=
Ignore the carry out (9th bit)
29
Codes
Decimal numbers are coded using binary bit patterns
Decimal
BCD (8421)
2421
Excess-3
Gray
0000
0000
0011
0000
0001
0001
0100
0001
0010
0010
1001
0011
0011
0011
0110
0010
0100
0100
0111
0110
0101
0101
1000
0111
0110
0110
1001
0101
0111
0111
1010
0100
1000
1000
1011
1100
1001
1111
1100
1101
30
15
BCD Addition
BCD addition can be carried out as follows:
4
+8
12
0100
+1000
1100
0110
1 0010
8
+9
17
1000
+1001
10001
0110
1 0111
31
Complements, Signed Arithmetic, Codes Book Sections
Complements and Signed Arithmetic are
covered in Chapter 1, Sections 1.5 through
1.7
32
16