chpter 2 4jun2014

Post on 18-Jul-2016

10 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

yea

TRANSCRIPT

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) :

Nu

mb

er

rep

rese

nta

tio

n fo

r 1

st1

7 v

alu

e o

f d

iffe

ren

t

rad

ix.

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

Digit’s position indicates its relative weight or

significance.

N=(an-1an-2 … a1a0 . a-1a-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 102 + 2 x 101 + 3 x 100 +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 = ai ri

i=-m

n-1

Eg:

1011.112 =

1x23 + 0x22 + 1x21 + 1x20 +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 = 1x22 + 0x21 + 1x20

= (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

11 8

+ 1 9

3 7

Example of Decimal Subtraction

1 108

- 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 Borro

w

0 0 0 0

1 1 0 0

1 0 1 0

0 1 1 1

1 1

+ 1

1 0

100

- 1

1

Binary Addition ExampleAdd the four numbers:

(101101)2, (110101)2, (001101)2 and (010001)2

1,1 1 1,10 1 11 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 ExampleSubtract (10111)2 from (1001101)2

11100 100 01

1001 100 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 decimalA B

0 0 0

0 1 0

1 0 0

1 1 1

Binary Multiplication ExampleMultiply (10111)2 by (1010)2

1 0 1 1 1

× 1 0 1 0

10 0 0 0 0

11 0 1 1 1

10 0 0 0 0

1 0 1 1 1

1 1 1 0 0 1 1 0

Binary Division ExampleDivide (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

1’s complement

2’s 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

1’s Complement Form

+ve numbers is represented the same

as in sign magnitude form

-ve numbers is the 1’s complement of

the positive number (invert all bits)

For eg, +25 in 8-bit 1’s complement:

-25 in 8-bit 1’s 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

2’s Complement Form

+ve numbers is represented the same

as in sign magnitude form

-ve numbers is the 2’s complement of

the positive number (1’s comp. than add

1)

For eg, +25 in 8-bit 2’s complement:

-25 in 8-bit 2’s complement:

0 0 0 1 1 0 0 1

0 0 0 1 1 0 0 1 +25

1 1 1 0 0 1 1 0 1’s comp.

1 1 1 0 0 1 1 1 -25

Decimal Number Value of

Signed Numbers

Sign Magnitude

Sign bit and calculate decimal of magnitude

1’s complement

Add terms in polynomial form with –ve

weight on the sign bit. Then, plus 1

2’s 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) 1’s complement form

c) 2’s 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

26 25 24 23 22 21 20

0 0 1 × 24 0 1 × 22 0 1 × 20

Decimal Number Value of

Signed Numbers (cont.)b) 10010101 as 1’s complement form

= -128 + 16 + 4 + 1 + 1

= -106

1 0 0 1 0 1 0 1

-27 26 25 24 23 22 21 20

1 × -27 0 0 1 × 24 0 1 × 22 0 1 × 20

Negative weight

for sign bit

Decimal Number Value of

Signed Numbers (cont.)c) 10010101 as 2’s complement form

= -128 + 16 + 4 + 1

= -107

1 0 0 1 0 1 0 1

-27 26 25 24 23 22 21 20

1 × -27 0 0 1 × 24 0 1 × 22 0 1 × 20

Negative weight

for sign bit

Signed Numbers Arithmetic

Only 2’s complement will be discussed since

it is the most widely used.

Range of 2’s complement signed number:

Range = -(2n-1) to (2n-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 2’s complement,125 + 58 =

1830 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 2’s complement For e.g., 8-bit binary 2’s 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 2’s 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 159710 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 0111So, 159710 = 0001 0101 1001 0111BCD

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 (0110BCD) 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 11

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)

top related