Vous êtes sur la page 1sur 53

Chapter 2

Number System
Number system
Base conversion
Why Learn Number System
Arithmetic
Sign Number
BCD and ASCII codes

Number Systems
Is an ordered set of symbols, called digits.
Has relationship between digits:- (+) (-
)(/)(x)
Radix (r) or base, of the number system is
the total number of single digits allowed in
the number system.
4 number systems commonly used in digital system
design and computer programming are:

Decimal ~> r = 10
Binary ~> r = 2
Octal ~> r = 8
Hexadecimal ~> r = 16

Decimal number system has 10 basic (base) digits:-

These are the only digits allowed to be used
repetitively to represent an amount in decimal
system.
As such, binary, octal and hexadecimal has these
symbols as their base digits:-

Binary (r = 2) : (commonly called bits)
Octal (r = 8) :
Hexadecimal (r = 16) :

N
u
m
b
e
r

r
e
p
r
e
s
e
n
t
a
t
i
o
n

f
o
r

1
s
t

1
7

v
a
l
u
e

o
f

d
i
f
f
e
r
e
n
t

r
a
d
i
x
.

R = 10 R = 8 R = 2 R = 16
0 0 0 0
1 1 1 1
2 2 10 2
3 3 11 3
4 4 100 4
5 5 101 5
6 6 110 6
7 7 111 7
8 10 1000 8
9 11 1001 9
10 12 1010 A
11 13 1011 B
12 14 1100 C
13 15 1101 D
14 16 1110 E
15 17 1111 F
16 20 10000 10
Why learn number systems???
Computers only understand binary (e.g.
1=HIGH/ON; 0=LOW/OFF)
Number systems are used to represent the binary
number.
For e.g.:
Colors (WWW, Photoshop/GIMP etc)
Assembly language
Imagine writing 111111111111111111111111 for
color white instead of FFFFFF or 255,255,255.
Number may have both integer and fractional
part, which are separated by a radix point (.).

Number may also be represented in positional or
polynomial notations.

Positional Notation
Digits position indicates its relative weight or
significance.
N=(a
n-1
a
n-2
a
1
a
0
. a
-1
a
-2
a
-m
)
r
Most significant digit
Least significant digit

Polynomial Notation
123.35 = 100 + 20 + 3 + 0.3 +0.05
= 1 x 100 + 2 x 10 + 3 x 1 + 3 x 0.1 + 5 x 0.01
= 1 x 10
2
+ 2 x 10
1
+ 3 x 10
0
+3 x 10
-1
+ 5 x 10
-2

Each digit resides in a weighted position
The weight of each position is a power of the radix
(radix 10 in this case)
N = a
i
r
i
i=-m

n-1

Eg:
1011.11
2
=
1x2
3
+ 0x2
2
+ 1x2
1
+ 1x2
0
+1x2
-1
+ 1x2
-2
Base Conversion
Base r Decimal
The conversion of a number in base r to decimal
is done by expanding the number in polynomial
notation and adding all the terms, e.g convert
(101)
2
to decimal system:

(101)
2
= 1x2
2
+ 0x2
1
+ 1x2
0

= (5)
10


Convert (274)
8
to base 10



Convert (1F.3)
16
to base 10
Decimal Base r
Two methods: Integer part
Fractional Part
Integer Part
divide the number and all successive quotients by r
and then groups the remainders.
E.g: convert (41)
10
to binary:

41 Remainder
41/2 = 20 1

20/2 = 10 0

Successive quotient 10/2 = 5 0

(quotient must be whole 5/2 = 2 1

number) 2/2 = 1 0

1/2 = 0 1

1 0 1 0 0 1


MSB LSB
Thus , (41)
10
= (101001)
2

Fractional Part
multiply the number by r and group the integers
instead of remainders.
new fraction is then multiplied by r again to obtain
another integer and new fraction. The procedure is
repeated until the fraction becomes 0 or the number
of digits has sufficient accuracy
E.g. Convert (0.6875)
10
to binary:


Integer

Fraction

Coefficient

0.6875 x 2 =
1.3750
1 + 0.3750

a
-1
= 1

0.3750 x 2 =
.7500
0 + 0.7500

a
-2
= 0

0.7500 x 2 =
1.500
1 + 0.5000

a
-3
= 1

0.5000 x 2 =
1.000
1 + 0.0000

a
-4
= 1

Therefore, (0.6875)
10
= (0.1011)
2

Zero fraction
Base Conversion (Binary-Octal-Hex)
To convert a binary number to octal or
hexadecimal, we can partition and group binary
numbers to three digits (for binary octal) and
four digits (binaryhexadecimal) starting from
the radix point.

E.g. convert 10110001101011.1111001 to octal
and hexadecimal system.

Partition & group
to 3 bits
: 0 10 110 001 101 011 . 111 100 100
Octal equivalent
of each binary
group
: 2 6 1 5 3 . 7 4 4
So, (10110001101011.11110010)
2
= ( 26153.744)
8

Binary Octal
Binary Hexadecimal
Partition & group
to 4 bits
: 0010 1100 0110 1011 . 1111 0010
Hexadecimal
equivalent of each
binary group
: 2 C 6 B . F 2
So, (10110001101011.11110010)
2
= ( 2C6B.F2)
16

Hexa, Octal Binary
To convert from Hexadecimal or Octal system to
Binary system, reverse the process above:

Convert each hexadecimal digit into 4 bits binary
number. The number in Hexadecimal system and the
Binary system must be equivalent when converted in
Decimal system.
Convert each octal digit into 3 bits binary number.
The number in Octal system and the Binary system
must be equivalent when converted in Decimal system.

Intro
Example of Decimal Addition
1
1
8
+ 1 9
3 7
Example of Decimal Subtraction
1


10
8
- 9
9



8+9 = 7 carry 1
borrow
Binary Addition/Subtraction
Addition Table
A + B

Subtraction Table
A - B
A B Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
A B Subt Borrow
0 0 0 0
1 1 0 0
1 0 1 0
0 1 1 1

1
1
+ 1
1 0

10
0
- 1
1
Binary Addition Example
Add the four numbers:
(101101)
2
, (110101)
2
, (001101)
2
and (010001)
2
10
10
1
10
0
10
1
1
1
10
0 1
1 1 0 1 0 1
0 0 1 1 0 1
+ 0 1 0 0 0 1
1 0 0 0 0 0 0 0
Note:
1 + 1 + 1 + 1 = 100
Binary Subtraction Example
Subtract (10111)
2
from (1001101)
2
1
1
10
0
10
0
0
1
10
0
1
10
0 1
- 1 0 1 1 1
1 1 0 1 1 0
Binary Multiplication/Division
Multiplication Table
A B

Division
Follows the same as division
in decimal
A B
0 0 0
0 1 0
1 0 0
1 1 1
Binary Multiplication Example
Multiply (10111)
2
by (1010)
2
1 0 1 1 1
1 0 1 0
1
0 0 0 0 0
1
1 0 1 1 1
1
0 0 0 0 0
1 0 1 1 1
1 1 1 0 0 1 1 0
Binary Division Example
Divide (1110111)
2
by (1001)
2
1 1 0 1
1 0 0 1 1 1 1 0 1 1 1
1 0 0 1
1 0 1 1
1 0 0 1
1 0 1 1
1 0 0 1
1 0 Remainder
Signed Binary Numbers
So far, we have only considered unsigned numbers.
But digital systems must be able to handle positive
and negative numbers.
Signed numbers consists of sign and magnitude
info.

Sign = 0 +ve
Sign = 1 -ve
Three forms:
Sign magnitude
1s complement
2s complement
Sign Magnitude Representation
Sign-Magnitude Form
Simple, but least used.
Left most bit sign bit. The rest are magnitude
For eg, +25 in 8-bit sign-magnitude form:

And -25 in the same form:

-ve number has the same magnitude bits as +ve,
but sign bit =1.
0 0 0 1 1 0 0 1
1 0 0 1 1 0 0 1
1s Complement Form
+ve numbers is represented the same as in
sign magnitude form
-ve numbers is the 1s complement of the
positive number (invert all bits)
For eg, +25 in 8-bit 1s complement:

-25 in 8-bit 1s complement:

0 0 0 1 1 0 0 1
0 0 0 1 1 0 0 1 +25
Invert bits
1 1 1 0 0 1 1 0 -25
2s Complement Form
+ve numbers is represented the same as in
sign magnitude form
-ve numbers is the 2s complement of the
positive number (1s comp. than add 1)
For eg, +25 in 8-bit 2s complement:

-25 in 8-bit 2s complement:

0 0 0 1 1 0 0 1
0 0 0 1 1 0 0 1 +25
1s comp. + 1
1 1 1 0 0 1 1 1 -25
Decimal Number Value of Signed
Numbers
Sign Magnitude
Sign bit and calculate decimal of magnitude
1s complement
Add terms in polynomial form with ve weight
on the sign bit. Then, plus 1
2s complement
Add terms in polynomial form with ve weight
on the sign bit.
Decimal Number Value of Signed
Numbers (cont.)
Determine the decimal value of the 8-bit binary
signed number 10010101 expressed in:
a) Sign magnitude form
b) 1s complement form
c) 2s complement form
Decimal Number Value of Signed
Numbers (cont.)
a) 10010101 as Sign magnitude
Sign bit = 1, so negative
Magnitude = 0010101.



= - (16 + 4 + 1)
= - 21

0 0 1 0 1 0 1
2
6
2
5
2
4
2
3
2
2
2
1
2
0
0 0 1 2
4
0 1 2
2
0 1 2
0

Decimal Number Value of Signed
Numbers (cont.)
b) 10010101 as 1s complement form



= -128 + 16 + 4 + 1 + 1
= -106

1 0 0 1 0 1 0 1
-2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1 -2
7
0 0 1 2
4
0 1 2
2
0 1 2
0

Negative weight
for sign bit
Decimal Number Value of Signed
Numbers (cont.)
c) 10010101 as 2s complement form



= -128 + 16 + 4 + 1
= -107

1 0 0 1 0 1 0 1
-2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1 -2
7
0 0 1 2
4
0 1 2
2
0 1 2
0

Negative weight
for sign bit
Signed Numbers Arithmetic
Only 2s complement will be discussed since it is
the most widely used.
Range of 2s complement signed number:
Range = -(2
n-1
) to (2
n-1
1); n=no. of bits
For n=8,
Range = -128 to 127
= (10000000)
2cns
to (01111111)
2cns

Signed Numbers Arithmetic (cont.)
Addition (A = B + C)
a) Both positive numbers
b) Positive number > negative number
c) Negative number > positive number
d) Both negative
Signed Numbers Arithmetic (cont.)
a) Both positive numbers
addition
7 + 4
b) +ve > -ve numbers
15 +(- 6)
0 0 0 0 0 1 1 1
+ 0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 1
0 0 0 0 1 1 1 1
+ 1 1 1 1 1 0 1 0
1 0 0 0 0 1 0 0 1
Discard carry
Signed Numbers Arithmetic (cont.)
c. -ve > +ve number
16 + -24
d) Both ve numbers
- 5 + (-9)
0 0 0 1 0 0 0 0
+ 1 1 1 0 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 1
+ 1 1 1 1 0 1 1 1
1 1 1 1 1 0 0 1 0
Discard carry
Signed Numbers Arithmetic (cont.)
Overflow condition
When two numbers added, result may exceed the
range.
Can only occur when both +ve or both ve numbers
Indicated by an incorrect sign bit
For eg, 8-bit binary 2s complement,125 + 58 = 183
0 1 1 1 1 1 0 1
+ 0 0 1 1 1 0 1 0
1 0 1 1 0 1 1 1
+ve plus +ve, but ve result
8-bit range =
-128 to 127.
But answer exceeds
Hence, OVERFLOW condition occurred
Signed Numbers Arithmetic (cont.)
Subtraction (A = B C)
Computation is treated as A = B + (-C)
Sign of C is changed by taking its 2s complement
For e.g., 8-bit binary 2s comp:
8 3 = 8 + (-3)

8 = (00001000)
2cns

3 = (00000011)
2cns
-3 = (11111101)
2cns





So, (00001000)
2cns
- (00000011)
2cns
=
(00000101)
2cns

0 0 0 0 1 0 0 0
+ 1 1 1 1 1 1 0 1
1 0 0 0 0 0 1 0 1
For e.g., 8-bit binary 2s comp:
12 (-9) = 12 + (9)

12 = (00001100)
2cns

-9 = (11110111)
2cns
9 = (00001001)
2cns





So, (00001100)
2cns
(11110111)
2cns
=
(00010101)
2cns

0 0 0 0 1 1 0 0
+ 0 0 0 0 1 0 0 1
0 0 0 1 0 1 0 1
Binary Coded Decimal (BCD)
A way to express EACH decimal digits (0 to 9),
with binary code.
Uses 4 bits binary for each decimal digit


Invalid codes : 1010 to 1111
For e.g., Encode 1597
10
in BCD:

Decimal 0 1 2 3 4 5 6 7 8 9
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
1 5 9 7
0001 0101 1001 0111
So, 1597
10
= 0001 0101 1001 0111
BCD
BCD Addition
1) Add the two BCD numbers as in binary
2) If 4-bit sum 9, valid number
3) If 4 bit sum > 9 OR carry generated, invalid.
Add 6 (0110
BCD
) to the 4-bit sum. If carry is
generated when adding 6, carry to next 4-bit sum
BCD Addition (cont.)
490 + 497
0 1 0 0
1
1 0 0 1 0 0 0 0
+ 0 1 0 0 1 0 0 1 0 1 1 1
1 0 0 1 1 0 0 1 0 0 1 1 1
+ 0 1 1 0
1 0 0 1 1 0 0 0 0 1 1 1
9 8 7
67 + 57
0 1 1 0 0 1 1 1
+ 0 1 0 1 0 1 1 1
1
1 0 1 1
1
1 1 1 0
+ 0 1 1 0 + 0 1 1 0
0 0 0 1 0 0 1 0 0 1 0 0
1 2 4
Gray Code
Exhibits only a single bit change from one
code to the next sequence.
No specific weight assigned to bit positions
Important for applications like shaft
position encoder that reduces error due to
high bit changes
Gray Code (cont.)
Decimal Binary Gray Code
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
7 0111 0100
8 1000 1100
9 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
Red marks bit change
American Standard Code for
Information Interchange (ASCII)
Alphanumeric code used in most computers
and other electronics devices
American Standard Code for
Information Interchange (ASCII)

American Standard Code for
Information Interchange (ASCII)

Vous aimerez peut-être aussi