The Need For Number System?

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 72

The need for Number system?

Digital systems are built from circuits that process binary digits0s and 1s
But very few real-life problems are based on binary numbers or
any numbers at all.
Correspondence between the binary digits processed by
digital circuits and real-life numbers and conditions must be
established.
1

Preliminaries
1. A number system consists of an order set of symbols (digits) with
relations defined for +,-,*, /

2. The radix (or base) of the number system is the total number of
digits allowed in the the number system.

Example, in decimal numbers, radix = 10,

digits allowed = 0,1, 2,,9


3. Common base include:

Binary: 0, 1

Octal: 0,1,..,7

Hexadecimal: 0,1,,9,A,B,C, D,E,F


2

Outline
Number Systems
Positional Notations
Number Systems & Base-R to Decimal Conversion
Conversion between bases
Binary Arithmetic Operations
How to represent Negative Numbers
Comparison of Sign-and-Magnitude and Complements

Positional Notations
Weighted-positional notation
Decimal number system, symbols = { 0, 1, 2, 3, ,
9}
Position is important
Example:(7594)10 = (7x103) + (5x102) + (9x101) +
(4x100)
The value of each symbol is dependent on its type
and its position in the number
In general, (anan-1 a0)10 = (an x 10n) + (an-1 x 10n-1)
+ + (a0 x 100)

Positional Notations
Fractions are written in decimal numbers after the
decimal point.
(2.75)10 = (2 x 100) + (7 x 10-1) + (5 x 10-2)
In general, (anan-1 a0 . f1f2 fm)10 = (an x 10n) +
(an-1x10n-1) + + (a0 x 100) + (f1 x 10-1) + (f2 x 10-2)
+ + (fm x 10-m)

Decimal (base 10) Number System


Weighting factors (or weights) are in powers-of-10:
103 102 101 100.10-1 10-2 10-3 10-4
To evaluate the decimal number 593.68, the digit in
each position is multiplied by the corresponding
weight:
5102 + 9101 + 3100 + 610-1 + 810-2
= (593.68)10

Base-R to Decimal Conversion


Binary (base 2): weights in powers-of-2.
Binary digits (bits): 0,1.
Octal (base 8): weights in powers -of-8.
Octal digits: 0,1,2,3,4,5,6,7.
Hexadecimal (base 16): weights in powers-of-16.
Hexadecimal digits:
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
Base R: weights in powers-of-R.

Base-R to Decimal Conversion


(1101.101)2 = 123 + 122 + 0+ 120 + 12-1 + 0+12-3
= 8 + 4 + 1 + 0.5 + 0.125 = (13.625)10

(572.6)8 = 582 + 781 + 280 + 68-1


= 320 + 56 + 16 + 0.75 = (392.75)10
(2A.8)16 = 2161 + 10160 + 816-1
= 32 + 10 + 0.5 = (42.5)10
(341.24)5 = 352 + 451 + 150 + 25-1 + 45-2
= 75 + 20 + 1 + 0.4 + 0.16 = (96.56)10

Decimal-to-Base R Conversion
Algorithm 1
Step 1: Break the number in two parts: Whole number
and fraction part.
Step 2: Repeated Division-by-R Method (for whole
numbers)

Step 3: Repeated Multiplication-by-R Method (for


fractions)

Algorithm 2: Sum-of-Weights Method

Repeated Division-by-R Method


To convert a whole number to base R, use successive
division by R until the quotient is 0. The remainders
form the answer, with the first remainder as the least
significant bit (LSB) and the last as the most significant
bit (MSB).
2 43
2 21 rem 1
LSB
(43)10 = (101011)2
2 10 rem 1
2
5 rem 0
2
2 rem 1
2
1 rem 0
0 rem 1
MSB
10

Repeated Multiplication-by-R Method


To convert decimal fractions to binary, repeated
multiplication by R is used, until the fractional product
is 0 (or until the desired number of decimal places).
The carried digits, or carries, produce the answer, with
the first carry as the MSB, and the last as the LSB.

E.g. (0.3125)10 = (.0101)2


0.3125 2=0.625
0.625 2=1.25
0.25 2=0.50
0.5 2=1.00

Carry
0
1
0
1

MSB

LSB
11

Sum-of-Weights Method
Determine the set of binary weights whose sum is
equal to the decimal number.
(9)10 = 8 + 1 = 23 + 20 = (1001)2
(18)10 = 16 + 2 = 24 + 21 = (10010)2
(58)10 = 32 + 16 + 8 + 2 = 25 + 24 + 23 + 21 = (111010)2
(0.625)10 = 0.5 + 0.125 = 2-1 + 2-3 = (0.101)2
12

Decimal to Binary Conversion


A SIMPLE TECHNIQUE

Example:- Convert 146 (decimal number) in to


binary
Write the corresponding weights in binary as
follows
256 128 64 32 16 8 4 2 1
Nearest small number to 146 is 128.write 1 at
this place as given below
13

1
256 128 64 32 16 8 4 2 1
Now subtract 128 from 146 which equals to
18.Again check the nearest small number to 18
is 16.Write 1 at this place and keep other
positions in between them as 0
1 0 0 1
256 128 64 32 16 8 4 2 1
14

Now subtract 16 from 18 , equals to 2.Place 1 at


this position and remember now remainder is
zero.

1 0 0 1 0 0 1 0
256 128 64 32 16 8 4 2 1
The output is

10010010
15

Summary:
Conversion between Bases
Base-R to decimal: multiply digits with their
corresponding weights
Decimal to binary (base 2)
whole numbers: repeated division-by-2
fractions: repeated multiplication-by-2
Decimal to base-R

whole numbers: repeated division-by-R


fractions: repeated multiplication-by-R
16

Conversion between Bases


In general, conversion between bases can be done via
decimal:
Base-2
Base-3
Base-4

Base-R

Decimal

Base-2
Base-3
Base-4
.
Base-R

Shortcuts are available for conversion between bases 2, 4, 8, 16.

17

Binary-Octal/Hexadecimal Conversion
Binary Octal: Partition in groups of 3
(10 111 011 001 . 101 110)2 = (2731.56)8
Octal Binary: reverse
(2731.56)8 = (10 111 011 001 . 101 110)2
Binary Hexadecimal: Partition in groups of 4
(101 1101 1001 . 1011 1000)2 = (5D9.B8)16 = 0x5D9.B8
Hexadecimal Binary: reverse
(5D9.B8)16 = (101 1101 1001 . 1011 1000)2

18

Binary Arithmetic operations


Addition

Subtraction

0+0 = 0

0-0 = 0

0+1 = 1

0-1 = 1 (after borrowing)

1+0 = 1

1-0 = 1

1+1 = 1 0 (carry)

1-1 = 0

Multiplication
0*0 = 0
0*1 = 0
1*0 = 0

1*1 = 1

19

Binary Arithmetic Operations (I)


ADDITION

Like decimal numbers, two numbers can be added by


adding each pair of digits together with carry
propagation.
Start at the right-most bit
Carry is carried to one position left.

(11011)2
+ (10011)2
(101110)2

(647)10
+ (537)10
(1184)10
20

Binary Arithmetic Operations (II)


(11011)2
+ (10011)2
(101110)2

Carries

1
0
0
1
0

0
1
1
0
1

0
1
0
1
0

1
0
0
1
0

1
1
1
1
1

0
1
1
0
1

21

Binary Arithmetic Operations (III)


SUBTRACTION
Two numbers can be subtracted by subtracting
each pair of digits together with borrowing,
where needed.
(11001)2
- (10011)2
(00110)2

(25)10
- (19)10
(06)10

22

Binary Arithmetic Operations (IV)


(11001)2
- (10011)2
(00110)2

Borrows

0
0
0
0
0

0
1
1
0
0

1
1
0
0
0

1
0
0
1
1

0
0
1
1
1

0
1
1
0
0

23

Binary Arithmetic Operations (V)


MULTIPLICATION
To multiply two numbers, take each digit of the multiplier and
multiply it with the multiplicand. This produces a number of
partial products which are then added.

(11001)2
x (10101)2

(214)10
x (152)10

(11001)2
(11001)2
+(11001)2
(1000001101)2

(428)10
(1070)10
+(214)10
(32528)10

Multiplicand
Multiplier
Partial
products
Result
24

Multiplication Using Add Shift

Multiply 12 and 9
12=1100
Q
9=1001
B
Assume A=0000 initially and form the table
A_ Q_ B

25

Check Qo If Qo=0------then shift A_Q


If Qo=1 then add A to B and then shift A_Q
No of steps = no. of digits in B=4
Note shift is equal to one step and if add is there
then add+shift is one step

26

Multiplication Using Add Shift

27

Result is A_Q=0110 1100


which is 0+64+32+0+8+4+0+0=108

28

Negative numbers representation


Till now, we have only considered how unsigned numbers can be
represented. There are four common ways of representing signed numbers:
Sign-and-Magnitude
Diminished radix (R-1s) complement ( e.g. 1s complement)
Radix (Rs) complement ( e.g. 2s complement)
Excess k
With signed magnitude representation, one has to look at both the signs
and magnitude of operands in an arithmetic operation. Hence, different
procedures (circuits) would have to be used for each case. Complement
representation takes care of this problem.
29

Negative Numbers: Sign-and-Magnitude (I)


Negative numbers are usually written by pre-pending a minus sign in front.
Example:
- (12)10 , - (1100)2
In computer memory of fixed width, this sign is usually represented by a bit:
0 for + and
1 for The left-most bit is the sign bit and the remaining bits hold the absolute
magnitude. memory of fixed
Example: an 8-bit number can have 1-bit sign and 7-bits magnitude.

sign

magnitude

30

Negative Numbers: Sign-and-Magnitude (II)


Largest Positive Number: 0 1111111

+(127)10

Largest Negative Number: 1 1111111

-(127)10

Zeroes:

0 0000000 +(0)10
1 0000000 -(0)10

Range: -(127)10 to +(127)10


To negate a number, just invert the sign bit.
Examples:
- (0 0100001)sm = (1 0100001)sm
- (1 0000101)sm = (0 0000101)sm
31

Complement representation
In complement representation, a positive number is
represented as it is (like an unsigned positive number)
A negative number, however, is represented by taking the
complement of unsigned number.
Complement numbers can help perform subtraction. With
complements, subtraction can be performed by addition.
A single circuit can be used to perform both addition and subtraction.

32

Complements
In general, for Base-r number N having n integer and m
fractional digits, there are:
(i) Diminished Radix (or r-1s) Complement

Nr1 r n N r m
(ii) Radix (or rs) Complement
Nr r n N
For Base-2 number, we have:
(i) 1s Complement: N1= 2n - |N| 2-m
(ii) 2s Complement: N2= 2n - |N|
33

1s Complement (I)

1,s complement of an unsigned number is obtained by


inverting all the bits of the number
Examples: 1s complement of (00000001)2 is
(11111110)1s
1s complement of (01111111)2 = (10000000)1s

Range of values: -(2n+m-1 1)10 R +(2n+m-1 1)


1,s representation of the number in 8-bits
Example:
1. (+14)10 = (00001110)2 = (00001110)1s
2. (-14)10 = -(00001110)2 = (11110001)1s
3. (-80)10 = (?) 2 = (?)1s

34

1s Complement (II)
For 8-bits number system:
Largest Positive Number:

0 1111111 +(127)10

Largest Negative Number:

1 0000000

Zeroes:

-(127)10

0 0000000
1 1111111

Range: -(127)10 to +(127)10


The most significant bit still represents the sign:
0 = +ve; 1 = -ve.

35

1s Complement (III)
Given a number x which can be expressed as an nbit binary number, its negative value can be obtained
in 1s-complement representation using:
- x = 2n - x - 1
Example: With an 8-bit number 00001100, its
negative value, expressed in 1s complement, is
obtained as follows:
-(00001100)2 = - (12)10
= (28 - 12 - 1)10
= (243)10
= (11110011)1s
36

2s Complement (I)
2s complement of a negative number is
obtained by inverting all the bits and adding 1.
Examples:
1. 2s complement of (00000001)2
= (11111110)1s
(invert)
= (11111111)2s (add 1)
2. 2s complement of (01111110)2
= (10000001)1s
(invert)
= (10000010)2s (add 1)

37

2s Complement (I)

2s complement representation for 8 bit numbers:

Example:
1. (+14)10 = (00001110)2 = (00001110)2s
2. (-14)10 = -(00001110)2 = (11110010)2s
3. (-80)10 = (?) 2 = (?)2s

38

2s Complement (II)
Given a number x which can be expressed as an n-bit
binary number, its negative number can be obtained in
2s-complement representation using:
- x = 2n - x
Example: With an 8-bit number 00001100, its negative
value in 2s complement is thus:
-(00001100)2 = - (12)10
= (28 - 12)10
= (244)10
= (11110100)2s
39

2s Complement (III)
Largest Positive Number:

0 1111111

Largest Negative Number: 1 0000000


Zero:

+(127)10
-(128)10

0 0000000

Range: -(128)10 to +(127)10

The most significant bit still represents the sign:


0 = +ve; 1 = -ve.

40

Comparisons of Sign-andMagnitude & Complements (I)


Example: 4-bit signed number (positive values)
Value

Sign-andMagnitude

1s
Comp.

2s
Comp.

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

0111
0110
0101
0100
0011
0010
0001
0000

0111
0110
0101
0100
0011
0010
0001
0000

0111
0110
0101
0100
0011
0010
0001
0000
41

Comparisons of Sign-and-Magnitude
and Complements (II)
Example: 4-bit signed number (negative values)
Value Sign-and1s
2s
Magnitude
Comp.
Comp.

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

1000
1001
1010
1011
1100
1101
1110
1111
-

1111
1110
1101
1100
1011
1010
1001
1000
-

1111
1110
1101
1100
1011
1010
1001
1000

MSB = 1 indicates a negative number in either notation


42

Review of what we have done


What are different number systems: decimal, binary, octal and hexadecimal
How to convert a number in one base to other base

Addition, subtraction etc. in binary and other bases


How to represent a negative number in binary base

What are we going to do now


Subtraction of numbers using Complement notation (this is easy method)
Fixed point notation and floating point notation
How to represent character such as A,~,! etc. in Computers
What if one or two bits have error? Employ Error detection codes for
storing numbers

43

Use of complements
Complement number system is used to minimize the

amount of circuitry needed to perform integer


arithmetic.
For example, A-B can be performed by computing A + (-B),
where (-B) is represented in 2s complement of B.
Hence, the computer needs only binary adder and
complementing circuit to handle both addition and subtraction

44

Overflow (I)
Signed binary numbers are of a fixed range.
If the result of addition/subtraction goes beyond this
range, overflow occurs.
Two conditions under which overflow can occur are:
(i) positive add positive gives negative
(ii) negative add negative gives positive

45

2s complement addition
Algorithm:
1. Perform binary addition on the two numbers.
2. Ignore the carry out of the MSB.
3. Check for overflow: Overflow occurs if the carrier into and
out of the MSB are different.

46

2s complement subtraction
Algorithm for performing A - B:
A-B = A + (-B)
1. Take 2s complement of B by inverting all the bits and
adding 1
2. Add the 2s complement of B to A

47

Examples: 2s addition/Subtraction
4-bits system
+3
+ +4
---+7
----

0011
+ 0100
------0111
------+6
+ -3
---+3
----

0110
+ 1101
------10011
-------

-2
1110
+ -6 + 1010
----------8
11000
-------------No Over Flow
+4
+ -7
----3
----

0100
+1 001
------1101
------48

Examples: Overflow in
2s addition/Subtraction
4-bits system
-3
+ -6
----9
----

1101
+ 1010
------10111
------+7

+5
+ +6
---+11
----

0101
+ 0110
------1011
-------5
49

1s complement
Addition/Subtraction rules
Algorithm C=A+B:
1. Perform binary addition on the two numbers
2. If there is a carry out of the MSB, add 1 to the result (to get C)

3. Check for overflow: if carried into and out of of MSB are different
and C is opposite sign of A and B
Algorithm A-B

1. Complement all bits of B


2. Proceed as addition
50

Examples: 1s addition/subtraction
+3
+ +4
---+7
----

0011
+ 0100
------0111
-------

-2
+ -5
----7
----

1101
+ 1010
------10111
+ 1
------1000

+5
+ -5
----0
----

0101
+ 1010
------1111
-------

-3
1100
+ -7 + 1000
----------10
10100
---+ 1
------0101 ov

51

Decimal number complements

9s complement of the decimal number N = (10n 1) N


= n (9s) N
i.e. {subtract each digit from 9}
Example 9s complement of 134795 is 865204
Similarly
1s complement of the binary number N = (2n -1) N = n
(1s) N
Example 1s complement of 110100101 is 001011010
which can be obtained by replacing each one by a zero and
each
zero by one.
52

Decimal number complements


rs complement:
10s complement of the decimal number N = 10n N = (r1)s complement + 1
Example 10s complement of 134795 is 865205
Example find the 9s and 10s complements of 314700.
Answer 9s complement = 685299
10s complement=685300
Rule: To find the 10s complement of a decimal number
leave all
leading zeros unchanged. Then subtract the first non-zero
digit
from 10 and all the remaining digits from 9s.
53

Fixed Point Numbers (I)


The signed and unsigned numbers representation given
are fixed point numbers.
The binary point is assumed to be at a fixed location,
say, at the end of the number:

binary point

can represent all integers between -128 to 127 (for 8 bits).

54

Fixed Point Numbers (II)


In general, other locations for binary points possible.

integer part

binary point

fraction part

Examples: If two fractional bits are used, we can represent:


(001010.11)2s = (10.75)10
(111110.11)2s = -(000001.01)2
= -(1.25)10
55

Floating Point Numbers (I)


Fixed point numbers have limited range.
To represent very large or very small numbers, we use
floating point numbers (cf. scientific numbers).
Examples:
0.23 x 1023 (very large positive number)
-0.1239 x 10-10 (very small negative number)

56

Floating Point Numbers (II)


Floating point numbers have three parts:
mantissa, base, and exponent
Base is usually fixed for each number system.
Therefore, needs only mantissa and exponent.

mantissa

exponent

57

Floating Point Numbers (III)


Mantissa is usually in normalised form:
(base 10) 23 x 1021 normalised to 0.23 x 1023
(base 10) -0.0017 x 1021 normalised to -0.17 x 1019
(base 2) 0.01101 x 23 normalised to 0.1101 x 22

A 16-bit floating point number may have 10-bit mantissa


and 6-bit exponent.
More bits in exponent gives larger range.
More bits for mantissa gives better precision.

58

Arithmetic with Floating Point Numbers (I)


Arithmetic is more difficult for floating point numbers.
MULTIPLICATION
Steps: (i) multiply the mantissa
(ii) add-up the exponents
(iii) normalise
Example:
(0.12 x 102)10 x (0.2 x 1030)10
= (0.12 x 0.2)10 x 102+30

= (0.024)10 x 1032

(normalise)

= (0.24 x 1031)10

59

Arithmetic with Floating Point Numbers (II)


ADDITION
Steps: (i) equalise the exponents

(ii) add-up the mantissa


(iii) normalise
Example:
(0.12 x 103)10 + (0.2 x 102)10
= (0.12 x 103)10 + (0.02 x 103)10 (equalise exponents)
= (0.12 + 0.02)10 x 103
(add mantissa)
= (0.14 x 103)10

60

Binary Coded Decimal (BCD) (I)


Decimal numbers are more natural to humans. Binary
numbers are natural to computers. Quite expensive to
convert between the two.

If little calculation is involved, we can use some


coding schemes for decimal numbers.
One such scheme is BCD, also known as the 8421
code.
Represent each decimal digit as a 4-bit binary code.

61

Binary Coded Decimal (BCD) (II)


Decimal digit
BCD
Decimal digit
BCD

0
0000
5
0101

1
0001
6
0110

2
0010
7
0111

3
0011
8
1000

4
0100
9
1001

Some codes are unused, eg: (1010)BCD, (1011) BCD, ,


(1111) BCD. These codes are considered as errors.

Easy to convert, but arithmetic operations are more


complicated.
Suitable for interfaces such as keypad inputs and
digital readouts.
62

Binary Coded Decimal (BCD) (III)


Decimal digit
BCD
Decimal digit
BCD

0
0000
5
0101

1
0001
6
0110

2
0010
7
0111

3
0011
8
1000

4
0100
9
1001

Examples:
(234)10 = (0010 0011 0100)BCD
(7093)10 = (0111 0000 1001 0011)BCD
(1000 0110)BCD = (86)10
(1001 0100 0111 0010)BCD = (9472)10
Notes: BCD is not equivalent to binary.
Example: (234)10 = (11101010)2
63

Alphanumeric Codes (I)


Apart from numbers, computers also handle textual
data.
Character set frequently used includes:
alphabets:
A .. Z, and a .. z
digits:
0 .. 9
special symbols:
$, ., ,, @, *,
non-printable:
SOH, NULL, BELL,
Usually, these characters can be represented using 7 or
8 bits.

64

Alphanumeric Codes (II)


Two widely used standards:
ASCII (American Standard Code for Information Interchange)
EBCDIC (Extended BCD Interchange Code)

ASCII: 7-bit, plus a


parity bit for error
detection (odd/even
parity).
EBCDIC: 8-bit code.

Character
0
1
...
9
:
A
B
...
Z
[
\

ASCII Code
0110000
0110001
...
0111001
0111010
1000001
1000010
...
1011010
1011011
1011100
65

Alphanumeric Codes (III)


ASCII table:
LSBs
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

000
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
O
SI

001
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US

010
SP
!

#
$
%
&

(
)
*
+
,
.
/

MSBs
011
100
0
@
1
A
2
B
3
C
4
D
5
E
6
F
7
G
8
H
9
I
:
J
;
K
<
L
=
M
>
N
?
O

101
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_

110
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o

1000001 (65
in decimal)
111
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL
66

Error Detection Codes (I)


Errors can occur data transmission. They should be
detected, so that re-transmission can be requested.
With binary numbers, usually single-bit errors occur.
Example: 0010 erroneously transmitted as 0011, or
0000, or 0110, or 1010.

For single-error detection, one additional bit is needed.

67

Error Detection Codes (II)


Parity bit.
Even parity: additional bit supplied to make total number of
1s even.
Character
ASCII Code
Odd parity: additional bit
0
0110000 1
1
0110001 0
supplied to make total number
...
...
Parity
of 1s odd.Example: Odd parity.
9
0111001 1
bits
:
A
B
...
Z
[
\

0111010 1
1000001 1
1000010 1
...
1011010 1
1011011 0
1011100 1

68

Error Detection Codes (III)


Parity bit can detect odd number of errors but not even
number of errors.
Example: For odd parity numbers,
10011 10001 (detected)
10011 10101 (non detected)
Parity bits can also be
applied to a block of data:

0110
0001
1011
1111
1001
0101

1
0
0
1
1
0

Column-wise parity

Row-wise parity

Sometimes, it is not enough to do error detection. We


may want to do error correction. Error correction can
69
be done but is more expensive.

Summary and a quick review


Conversion between Bases
Binary-Octal/Hexadecimal Conversion

Binary Arithmetic Operations


Negative Numbers
Sign-and-Magnitude
1s Complement
2s Complement
Comparison of Sign-and-Magnitude and Complements

70

Summary and a quick review

Complements
Diminished-Radix Complements
Radix Complements
Subtraction using 2s-complement
Subtraction using 1s-complement
Overflow
Fixed-Point Numbers
Floating-Point Numbers
Arithmetic with Floating-Point Numbers

71

Summary and a quick review


Representing information
Codes
Binary Coded Decimal (BCD)
Alphanumeric Codes
Error Detection Codes

Read Chapter 1 of Nelsons textbook


72

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy