Вы находитесь на странице: 1из 69

Chapter 2

Section 1: Numbering Systems


Section 2: Binary Arithmetic
Section 3: Binary Codes

Section 1:
NUMBERING SYSTEMS

Basic Numbering Systems


4 basic numbering systems used in digital
systems:

i.
ii.
iii.
iv.

Decimal system
Binary system
Octal system
Hexadecimal system

Decimal System

Named as base 10 system


10 digits are possible
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Any

number of any magnitude can be


expressed by using the system of
positional weighting
102

101

100

100

10

MSD

.
Radix
point

10-1

10-2

0.1

0.01
LSD
4

Example of Decimal System


2.310 = 2x100 + 3x10-1
8610 = 8x101 + 6x100
632110 = 6x10? + 3x10? + 2x10? + 1x10?
562.3210 = 5x10? + 6x10? + 2x10? + 3x10?
+ 2x10?

Binary System

Named as base 2 system


2 digits are possible
0, 1

Any

number of any magnitude can be


expressed by using the system of
positional weighting
22

21

20

MSB

.
Radix
point

2-1

2-2

0.5

0.25
LSB
6

Example of Binary System


Consider 4-bit binary number, it ranges
from 00002 to 11112, i.e. 010 to 1510

Decimal

23

22

21

20

10

11

12

13

14

15

1
8

Octal System

Named as base 8 system


2 digits are possible
0, 1, 2, 3, 4, 5, 6, 7

Any

number of any magnitude can be


expressed by using the system of
positional weighting
82

81

80

64

MSD

.
Radix
point

8-1

8-2

1/8

1/64
LSD
9

Hexadecimal System

Named as base 16 system


2 digits are possible
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Any

number of any magnitude can be


expressed by using the system of
positional weighting
162

161

160

256

16

MSD

.
Radix
point

16-1

16-2

1/16

1/256
LSD
10

Decimal

Binary

Octal

Hexadecimal

0000

0001

0010

0011

0100

0101

0110

0111

1000

10

1001

11

10

1010

12

11

1011

13

12

1100

14

13

1101

15

14

1110

16

15

1111

17

F
11

Base Conversion
Convert a number from one number
system to another system

A number can be divided into two parts,


e.g.,
562.32

Integral Part

Fraction Part

12

i. Binary-to-Decimal Conversion
Summing the weight of all positions that
contain a 1
100112
= 2 4 + 21 + 2 0
= 16 + 2 + 1
= 1910
101110012
= 2 7 + 25 + 24 + 23 + 20
= 128 + 32 +16 + 8 + 1 = 18510

13

ii. Decimal-to-Binary Conversion


Method 1: Reverse of Binary-to-Decimal
Method
1910
= 16 + 2 + 1
= 24 + 21 + 20 = 100112

10.37510
= 8 + 2 + 0.25 + 0.125
= 23 + 21 + 2-2 + 2-3 = 1010.0112

14

ii. Decimal-to-Binary Conversion


Method 2: Repeated Division and
Multiplication Method
10.37510
= 1010.0112

Integral Part
2|10
2| 5 R = 0
2| 2 R = 1
2| 1 R = 0
0 R=1

Fraction Part
0.375 x 2 = 0.75
0.75 x 2 = 1.5
0.5 x 2 = 1.0

15

iii. Octal-to-Binary Conversion


Also

known as a 3-bit binary number


Each octal digit can be represented into
3-bit binary number
Octal

Binary

000

001

010

011

100

101

110

111

5638
= 101 110 0112

16

iv. Binary-to-Octal Conversion


11011011102
= 001 101 101 1102 = 15568
0.001 010 1102
= 0.001 010 1102 = 0.1268

1101101110.0010101102
1.
2.
3.
4.

Start from binary point


Divide it into 3-bit zones
Add 0s if lesser than 3-bit
Replace every 3-bit with octal digit

17

v. Decimal-to-Octal Conversion
Repeated Division and Multiplication
Method
170.3812510
= 252.303148

Integral Part
8|170
8| 21 R = 2
8| 2 R = 5
0 R=2

Fraction Part
0.38125 x 8 = 3.05
0.05 x 8 = 0.4
0.4 x 8 = 3.2
0.2 x 8 = 1.6
0.6 x 8 = 4.8
18

vi. Octal-to-Decimal Conversion


Summing the weight of all positions that
contain a nonzero digit

50.078
= 5x81 + 7x8-2
= 40 + 0.109375
= 40.10937510

19

vii. Hexadecimal-to-Decimal
Conversion
Summing the weight of all positions that
contain a nonzero digit

50EF.0A16
= 5x163 + Ex161 + Fx160 + Ax16-2
= 5x163 + 14x161 + 15x160 + 10x16-2
= 20480 + 224 + 15 + 0.039
= 20719.03910

20

viii. Decimal-to-Hexadecimal
Conversion
Repeated Division and Multiplication
Method
400.53410
= 190.88B16

Integral Part
16|400
16| 25 R = 0
16| 1 R = 9
0 R=1

Fraction Part
0.534 x 16 = 8.544
0.544 x 16 = 8.704
0.704 x 16 = 11.264

21

ix. Hexadecimal-to-Binary
Conversion
Also

known as a 4-bit binary number


Each hexadecimal digit can be
represented into 4-bit binary number
Hexad
ecimal

Binary

0000

0001

0010

0011

0100

0101

0110

0111

Hexad
ecimal

Binary

1000

1001

1010

1011

1100

1101

1110

1111

56316
= 0101 0110 00112

22

x. Binary-to-Hexadecimal
Conversion
11011011102
= 0011 0110 11102 = 36E16
0.0010101112
= 0.0010 1011 10002 = 0.2B816

1101101110.0010101112
1.
2.
3.
4.

Start from binary point


Divide it into 4-bit zones
Add 0s if lesser than 4-bit
Replace every 4-bit with hexadecimal digit

23

xi. Hexadecimal-to-Octal
Conversion
Method 1 (Faster): Convert to binary and
followed by octal
Method 2 (Slower): Convert to decimal
and followed by octal

12EF16
= 0001 0010 1110 11112
= 113578

24

Exercise
Convert (34567.12)8 to decimal
Convert (1234.687)10 to binary
Convert (F80B)16 to octal

25

Section 2:
BINARY ARITHMETIC

26

Binary Arithmetic

Basic in understanding digital systems

i.

Binary Addition
Binary Subtraction
Binary Multiplication
Binary Division

ii.
iii.
iv.

27

Binary Addition
Addition

table

0+0=0
0+1=1
1+0=1
1 + 1 = 0 carry 1
11012
+ 10112
-------110002

1310
+ 1110
------2410

Checking/Not steps

augend
addend
sum
28

Binary Subtraction

Subtraction table
0-0=0
0 - 1 = 1 borrow 1
1-0=1
1-1=0

111012
100112
-------010102

2910
- 1910
------1010

Checking/Not steps

minuend
subtrahend
difference
29

Binary Multiplication

Multiplication table
0x0=0
0x1=0
1x0=0
1x1=1

30

Binary Multiplication
Checking/Not steps

1101
x 1011
-----1101
1101
0000
1101
-----------10001111

1310
1110
-----1310
1310
------14310

multiplicand
multiplier

partial products

final product
31

Binary Division

Checking/Not steps

Divide 14510 by 1110 in binary


quotient
divisor

dividend

remainder
32

Exercise
Perform (56)10+(95)10 in binary
Perform (1101)2 X (1011)2 in binary
Perform (1001)2 (0111)2 in binary
Perform (1100)2 / (101)2 in binary

33

Example: 910
MSB: most significant bit
(left most bit)

LSB: least significant bit


(right most bit)

4-bit representation:
1001 2 (bit position 3 to 0)
5-bit representation:
0 1001 2 (bit position 4 to 0)
6-bit representation:
00 1001 2 (bit position 5 to 0)
7-bit representation: 000 1001 2 (bit position 6 to 0)
8-bit representation: 0000 1001 2 (bit position 7 to 0)

34

Sign Number Representation


Represent both positive and negative
numbers
MSB: as a sign bit
MSB = 0 indicates positive number
MSB = 1 indicates negative number
3 different representations:

i. Sign and magnitude system


ii. 1s compliment system
iii. 2s compliment system
35

i. Sign and Magnitude System


MSB = 0 indicates positive number
MSB = 1 indicates negative number
Remaining bits = magnitude of the
Sign Magnitude
number
(8-bit) 2510 = 000110012
(8-bit) -2510 = 100110012

Disadvantage: logic circuits are hard to


design as subtraction is needed

36

ii. 1s Complement System


Positive number: same as sign and
magnitude system
Negative number=> x = f(x) = (2n -1) x where n is the number of bits
(8-bit) -2510

2510 = 000110012
-2510 = 1111 11112 000110012
= 111001102

37

ii. 1s Complement System


Faster way: complement/flip every single
bit
(8-bit) -2510

2510 = 000110012
-2510 = 111001102

38

iii. 2s Complement System


Positive number: same as sign and
magnitude system
Negative number=> x = f(x) = 2n x
= (2n-1) x + 1 = x + 1
(8-bit) -2510

2510 = 000110012
-2510 = 1000000002 000110012
= 111001112

39

iii. 2s Complement System


Faster way: Start from LSB, leave all bits
unchanged until the first 1 is reached and
thereafter complement each bit
(8-bit) -2510

2510 = 000110012

-2510 = 111001112
Another

way: Perform 1s complement


followed by adding 1
40

Remark
To

obtain the magnitude of a negative


number represented with 1s complement
system:
Perform 1s complement on the negative
number

To

obtain the magnitude of a negative


number represented with 2s complement
system:
Perform 2s complement on the negative
number
41

Example
Find the negative integer represented in
1s complement as 111011002

11101100
1s complement:

00010011 = 1910

The number is -1910.

42

Example
Find the negative integer represented in
2s complement as 111011002

11101100
2s complement:

00010100 = 2010

The number is -2010.

43

Exercise
Express number -16324010 in 2s
complement using 20 bits.

44

Binary Addition of 2s Complement


Numbers
Subtraction is a special case of addition where

A B = A + (-B)
Any carry from sign position is ignored such
that an addition of two n-bit numbers will result
an n-bit output
An
overflow happens if the correct
representation of the sum (including sign)
requires more than n bits, i.e.,
Addition of two positive numbers gives a negative
output
Addition of two negative numbers gives a negative
45
output

2s Complement: Addition
CASE 1: Addition of 2 positive numbers, sum < 2n 1

CASE 2: Addition of 2 positive numbers, sum 2n 1

46

2s Complement: Addition
CASE 3: Addition of positive and negative numbers
(negative number has greater magnitude).
+5
0101
-6
1010
-----------------1
1111 (correct answer)

CASE 4: Same as case 3 except positive number has


greater magnitude
-5
1011
+6
0110
----------------+1 (1)0001 correct answer when carry from sign bit is
.
ignored (this is not an overflow)
47

2s Complement: Addition
CASE 5: Addition of two negative numbers,
|sum| 2n 1.
-3
1101
-4
1100
----------------- 7 (1)1001 correct answer when last carry is
ignored (this is not an overflow)

CASE 6: Addition of two negative numbers,


| sum | > 2n 1.
-5
1011
-6
1010
----------------- 11 (1)0101 wrong answer because of overflow (-11
requires 5 bits including sign)

48

1s Complement: Addition
CASE 1: Addition of 2 positive numbers, sum < 2n 1.

CASE 2: Addition of 2 positive numbers, sum 2n 1

49

CASE 3: Addition of positive and negative numbers


(negative number has greater magnitude).
+5
0101
-6
1001
-----------------1
1110 (correct answer)

CASE 4: Same as case 3 except positive number has


greater magnitude.
-5
1010
+6
0110
----------------(1)0000
1 (end-around carry)
----------------------0001 (correct answer, no overflow)
50

CASE 5: Addition of two negative numbers,


|sum| 2n 1.
-3
1100
-4
1011
----------------(1)0111
1 (end-around carry)
------------------7
1000 (correct answer, no overflow)
CASE 6: Addition of two negative numbers,
| sum | > 2n 1.
-5
1010
-6
1001
------------------(1)0011 (end-around carry)
1
------------------0100 (wrong answer due to overflow)

51

Exercise
Add

the following numbers, with those


negative represented in 6-bit 2s
complement. Indicate if an overflow.

a)
b)

c)
d)

25 + 15
25 + (-15)
-25 + 15
-25 + (-15)
52

2510 = 0110012
-2510 = 1001112
1510 = 0011112
-1510 = 1100012

2s: Ignore carry from the sign


position (that is bit-n)

2510 + 1510 = 0110012 + 0011112 = 1010002 (overflow)


2510 - 1510 = 0110012 + 1100012 = 0010102
-2510 + 1510 = 1001112 + 0011112 = 1101102
-2510 - 1510 = 1001112 + 1100012 = 0110002 (overflow)

53

Exercise
Add

the following numbers, with those


negative represented in 6-bit 1s
complement. Indicate if an overflow.

a)
b)

c)
d)

25 + 15
25 + (-15)
-25 + 15
-25 + (-15)
54

2510 = 0110012
-2510 = 1001102
1510 = 0011112
-1510 = 1100002

1s: Add carry from the sign


position to LSB

2510 + 1510 = 0110012 + 0011112 = 1010002 (overflow)


2510 - 1510 = 0110012 + 1100002 = 0010102
-2510 + 1510 = 1001102 + 0011112 = 1101012
-2510 - 1510 = 1001102 + 1100002 = 0101112 (overflow)

55

Section 3:
BINARY CODES

56

Binary Codes
All

input/data and instruction must be


represented into binary number for
processing
Weighted code: Binary Coded Decimal
Non weighted code: Gray Code
Error detection code: Even Parity, Odd
Parity

57

Binary Coded Decimal (BCD)


Straight assignment of the binary
equivalent
Each decimal digit is represented into 4bit binary number
The weights is 8, 4, 2, 1

58

Decimal Digit

BCD/8421 Code

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

9 3 7. 2 5

1001 0011 0111 . 0010 0101


59

BCD

The codes 1010 through 1111


are not used in BCD.

Decimal

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Binary

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

BCD

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
0001 0000
0001 0001
0001 0010
0001 0011
0001 0100
0001 0101
60

BCD Addition
Only 0000 to 1001 are valid
Any addition output larger than 1001,
add 0110 to correct it

Example:

4
+5
--9

0100
+ 0101
------1001

No Correction Needed
61

BCD Addition: More examples


5
+9
--14

0101
+ 1001
------1110 Invalid Code
+ 0110 Correction
Add 6
------1 0100= 14

8
+9
--17

1000
+ 1001
------1 0001 Invalid Code
+ 0110 Correction
Add 6
--------1 0111 = 17

62

BCD Addition: More examples


Perform 18410 + 57610 in BCD

184
+ 576
------

1
1
Carry
0001 1000 0100
+ 0101 0111 0110
----------------------0111 10000 1010 Invalid
+
0110 0110 Add 6
----------------------0111 0110 0000
= 760
63

Exercise
Add the BCD numbers: 0001 0110 + 0001 0101

64

Binary Codes for Decimal Digits


Decimal Digit

8-4-2-1 Code (BCD)

Gray Code

0000

0000

0001

0001

0010

0011

0011

0010

0100

0110

0101

0111

0110

0101

0111

0100

1000

1100

1001

1101

65

Gray Code
An

unweighted code
Two successive codes
differ in 1 bit only
Applicable to prevent
more than one bit
changes at a time
The only change

0010
0110

Decimal

Binary

Gray code

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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

0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
66

Binary to Gray Code Conversion


MSB is the same as MSB in binary
Go from left to right, compare adjacent pair of
binary bits to get the next Gray code bit. If they
are the same, the Gray code bit is 0, else it is 1.

Example: 10111012 = ?

1 0 1 1 1 0 1

Binary

1 1 1 0 0 1 1

Gray code
67

Gray Code to Binary Conversion


MSB is the same as MSB in binary

Go from left to right, compare the binary bit


generated with the next Gray code bit. If they
are the same, the Gray code bit is 0, else it is 1.

Example: 11010110Gray = ?

1 1 0 1 0 1 1 0

Gray Code

1 0 0 1 1 0 1 1

Binary
68

Error Detection Code


Parity method is used to detect simple transmission

errors involving one bit (or an odd number of bits)


A parity bit is an extra bit attached to a group of bits
to force the number of 1s to be either even (even
parity) or odd (odd parity).

Data = 1000001

Even parity
01000001

Odd parity
11000001

Parity bit added

A given system operates with even or odd parity, but not


both.

69

Вам также может понравиться