You are on page 1of 55

CHAPTER 2

NUMBER SYSTEMS, OPERATIONS


AND CODES

1
NUMBER SYSTEMS
Number systems in digital technique:
Decimal numbers based 10

Binary numbers based 2

Octal numbers based 8

Hexadecimal numbers based 16

2
Decimal numbers
Based on ten digits : 0,1, 2, 3, 4, 5, 6, 7, 8, 9 .

The position of each digit - assigned a weight.

The weight are positive powers-of-ten - increase from right to left,


105 104 103 102 101 100

For fractional numbers, the weight are negative powers-of-ten


- increase from the left to right ;
.10-1 10-2 10-3

Counting in decimal start at 0 to 9, 10 to 19, 20 to 29,100 to 199,


..1000 to 1999, and so on.

The value of a decimal number is the sum of the digits after each digit
has been multiplied by its weight, as examples 2.1 and 2.2.

3
Examples 2.1 & 2.2

Express the decimal number 36 as a sum of the values of each digit.


Solution: The digit 3 has a weight of 10 (101), as indicated by its position.
The digit 6 has a weight of 1 (100), as indicated by its position.

36 = (3x101) + (6x100)
= (3x10) + (6x1) = 30 + 6

Express the decimal number 789.21 as sum of the values of each digit.
Solution: The whole number digit 7 has a weight of 100 (102), the digit 8
has a weight of 10 (101), the digit 9 has a weight of 1 (100), the fractional
digit 2 has a weight of 0.1 (10-1), and the fractional digit 1 has a weight of
0.01 (10-2).

789.21 = (7x102) + (8x101) + (9x100) +(2x10-1) +(1x10-1)


= (7x100) + (8x10) + (9x1) + (2x0.1) + (1x0.01)
= 700 +80 + 9 + 0.2 + 0.01
4
Binary Numbers
Consist of two digits (bits): 0 and 1.
2 level logic: low level -digit 0
high level - digit 1.

The position of a 1 or 0 - indicates its weight.


The weights are based on powers-of-two:
2n-123 22 21 20.2-1 2-2 2-n

Counting in binary:
0, 1, 10, 11, 100, 101, 110, 111, 1000, , 1111.

The bit change start from the lowest bit.


The max. value is all the bits are 1. Exp: 5 bits, the max value= 11111.
With n-bits we can count up to a number equal to 2n 1.

Largest decimal number = 2n 1

5
Table 2-1: A Binary count zero through fifteen

6
Table 2-2: Binary Weight

POSITIVE POWERS OF TWO NEGATIVE POWERS OF TWO


(WHOLE NUMBERS) (FRACTIONAL NUMBER)

27 26 25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5 2-6

128 64 32 16 8 4 2 1 1/2 1/4 1/8 1/16 1/32 1/64

7
Binary to Decimal Conversion
Adding the weights of all bits that are 1 and discarding the
weights of all bits that are 0.

Convert the binary whole number 10111 to decimal.


Solution:

10111 = (1 X 24 ) + (0 X 23 ) + (1 X 22 ) + (1 X 21 ) + (1 X 20 )
= 16 + 0 + 4 + 2 + 1 = 23

Convert the fractional binary number 0.1101 to decimal.


Solution:

0.1101 = (1 X 2-1 ) + (1 X 2-2 ) + (0 X 2-3 ) + (1 X 2-4 )


= 0.5 + 0.25 + 0 + 0.0625 = 0.8125
8
Decimal-to-Binary Conversion
Determine the binary weights whose sum is equal to the decimal number.

Sum-of-weight method
Binary weights
256 128 64 32 16 8 4 2 1
357 = 256 + 64 + 32 + 4 + 1 101100101

Binary weights
1024 512 256 128 64 32 16 8 4 2 1
1937 = 1024 + 512 + 256 + 128 + 16 + 1 11110010001

9
Repeated division-by-2 method
PROCEDURE:

To convert decimal number 19 to binary, begin by dividing 19 by 2.

-Then divide each resulting quotient by 2 until there is a 0 whole


number quotient.

The remainders generated - form the binary number.

-The first remainder - is the least significant bit (LSB)


The last remainder - is produced is the most significant bit (MSB).

This procedure is shown in the following steps for converting the


decimal number 19 to binary.

10
Repeated division-by-2 method

11
Converting Decimal fractions to Binary
Using Sum-of-weights
Binary weights
64 32 16 8 4 2 1 .5 .25 .125 .0625
95.6875 = 64 + 16 + 8 + 4 + 2 + 1 + .5 + .125 + .0625
1011111.1011
Repeated division by 2 yields the whole number while repeated
multiplication by 2 of the fraction yields the binary fraction.

12
Repeated Multiplication by 2
Begin by multiplying decimal number by 2.

Then multiplying each resulting fractional part of


product by 2 until the fractional product is zero,

or until the desired number is reached.

The carries generated - produce the binary number.

The first carry produced is the MSB.

13
Repeated Multiplication by 2
MSB LSB
Example: .0 1 1
Carry
0.375 x 2 = 0.75 0

0.75 x 2 = 1.50 1

0.5 x 2 = 1.00 1

Stop when fractional part is all zeros.

14
Binary Addition
0 + 0 = 0 Sum of 0 with a carry of 0
0 + 1 = 1 Sum of 1 with a carry of 0
1+0=1 Sum of 1 with a carry of 0
1 + 1 = 10 Sum of 0 with a carry of 1
11001
+1101
100110

15
Binary Subtraction
0-0=0
1-1=0
1-0=1
10 -1 = 1 0 -1 with a borrow of 1
1011
-111
100

16
Hexadecimal Numbers

Composed of ten numeric digits (0 9) and six alphabetic characters


(A, B, C, D, E and F)

Each hexadecimal digit represents a 4-bit binary number (Table 2-3).

Base of sixteen: have subscript 16 - to avoid confusion with octal or


decimal numbers.

Each digit is an increasing power-of-sixteen.


. 163 162 161 160. 16-1 16-2 16-3

Counting in hexadecimal number:


10, 11, 12, 13, 14, 15, 16, 17, 18, 19. 1A, 1B, 1C, 1D, 1E, 1F, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 2A, 2B, 2C, 2D, 2E, 2F, 30, 31, .

17
Table 2-3

Decimal Binary Hexadecimal

0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

18
Binary to hexadecimal Conversion
Break the binary number into 4-bit groups, starting the right-most bit
and replace 4-bit group with the equivalent hexadecimal symbol.

Convert the following binary numbers to hexadecimal:


(a) 1110100101010110 (b) 1101010001110011011

Solution: (a) 1110 1001 0101 0110

E 9 5 6 = E956 16

(b) 0110 1010 0011 1001 1011

6 A 3 9 B = 6A39B 16

19
Hexadecimal to binary Conversion
Reverse the process and replace each hexadecimal symbol with the
appropriate four bits as illustrated below.

Determine the binary numbers for the following hexadecimal numbers:


(a) DA216 (b) 4C8E16

Solution: (a) D A 2

1101 1010 0010 = 1101101000102

(b) 4 C 8 E

0100 1100 1000 1110 = 1001100100011102

20
Hexadecimal to Decimal Conversion
Method 1: First convert the hexadecimal number to binary and then convert
from binary to decimal.

Determine the decimal numbers for the following hexadecimal numbers:


(a) E316 (b) 78916

Solution: (a) E 3

1110 0011 = 111000112 = 27 + 26 + 25 + 21 + 20 = 22710

(b) 7 8 9

0111 1000 1001 = 111100010012

= 210 + 29 + 28 + 27 + 23 + 20 = 192910
21
Hexadecimal to Decimal Conversion
Method 2: Multiply the decimal value of each hexadecimal digit by its weight
and then take the sum of these products.

Determine the decimal numbers for the following hexadecimal numbers:


(a) 3A16 (b) 84516

Solution: (a) 3A16 = (3 x 161) + (A x 160) = (3 x 16) + (10 x 1) = 5810

(b) 84516 = (8 x 162) + (4 x 161) + 5 x 160)

= (8 x 256) + (4 x 16) + (5 x 1) = 211710

22
Decimal to Hexadecimal Conversion
Repeated division of a decimal number by 16;
- the remainders formed the equivalent hexadecimal number
The first remainder produced is the least significant digit (LSD).

Convert the decimal number 126 to hexadecimal by repeated division by 16.

Solution: Hexadecimal
remainder
126/16 = 7.875 0.875 x 16 = 14 = E

7/16 = 0.4375 0.4375 x 16 = 7 = 7

Stop when whole


number quotient 7 E Hexadecimal
is zero. Number
MSD LSD
23
Hexadecimal Addition

Use the following rules to adding two


hexadecimal numbers;
In any given column, think of the two hexa digits in
term of their decimal values. Exp: 516 = 510 and C16 =
1210.
If sum of these two digits is 1510 or less, bring down
the corresponding hexa digit. EX 15 + 91=A6
If the sum of these two digits is greater than 1510,
bring down the amount of the sum that exceeds 1610
and carry a 1 to the next column. EX: 15F EX :
15
+ A7 + F5
206 10A24
Octal Numbers

Has eight digits; 0, 1, 2, 3, 4, 5, 6, 7.

Base of eight, each digit is an increasing power-of-eight.


. 83 82 81 80. 8-1 8-2 8-3

Use the subscript 8 to indicate an octal number.

Each octal digit represents a 3-bit binary number.

Counting in octal is similar to counting decimal, except that the digits 8


and 9 are not used.

To count above 7, we begin another column and start over;


..,6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21.

25
Octal to Decimal Conversion
Multiplying each digit by its weight and summing the products.

Converts each of the following octal numbers to decimal.


(a) 1678 (b) 37 8

Solution:

(a) Weight : 82 81 80
Octal number: 1 6 7

1678 = (1 x 82) + (6 x 81) + (7 x 80) = 64 + 48 + 7 = 11910

(b) 398 = (3 x 81) + (7x 80) = 24 + 7 = 3110

26
Decimal to Octal Conversion
Used repeated division-by-8 method, which is similar to the method used in the
conversion of decimal numbers to binary or to hexadecimal.

Convert the decimal number 228 to octal numbers by repeated division by 8.

Solution: Octal
remainder
228/8 = 28.5 0.5 x 8 = 4

28/8 = 3.5 0.5 x 8 = 4

3/8 = 0.375 0.375 x 8 = 3

Stop when whole


number quotient is zero. 3 4 4 Octal
Number
MSD LSD 27
Octal to Binary Conversion
Each octal digit is represented by 3bit binary number as shown in table below.

Octal digit 0 1 2 3 4 5 6 7
Binary 000 001 010 011 100 101 110 111

Example:
Convert each of the following octal numbers to binary.
(a) 738 (b) 240 8

Solution:
(a) 7 3 (b) 2 4 0

111 011 010 100 000

=1110112 =101000002

28
Binary to Octal Conversion
Reverse the process of octal to binary conversion.
Start with the right-most group of 3 bits and moving from right to left,
convert each 3-bit group to equivalent octal digit.

Convert each of the following binary numbers to octal:


(a) 1010012 (b) 10101112

Solution:

(b) 101 001 (b) 001 010 111

5 1 1 2 7

= 518 = 1278

29
Octal Addition

Use the following rules to adding two


octal numbers;
In any given column, think of the two octal digits in
term of their decimal values. Exp: 58 = 510.
If sum of these two digits is 710 or less, bring down
the corresponding octal digit. Ex: 365 + 12 =377
If the sum of these two digits is greater than 710,
bring down the amount of the sum that exceeds 810
and carry a 1 to the next column. Ex: 367
+715
1304

30
Binary Coded Decimal (BCD)
There are 2 types of binary coded:

1. Weighted Code :
- Each digit carries a weight based on its position relative.
- known as BCD each decimal digit (0-9) represent by a 4-bits
binary code.
- BCD 8421, BCD 84-2-1, & BCD addition

2. Unweighted Code :
- The bit positions in the code group do not have any specific weight
assigned to them.
- Excess-3, Gray

3. Alphanumeric codes

- ASCII, Extended ASCII, EBCDIC


31
BCD 8421 Code
The designation 8421 - indicates the binary weights (23, 22,21,20).
The decimal to BCD conversion is shown in table below.

Decimal Digit 8421


0 0000
1 0001 Invalid code
2 0010 Decimal Digit 8421
3 0011 10 1010
4 0100 11 1011
5 0101 12 1100
6 0110 13 1101
7 0111 14 1110
8 1000 15 1111
9 1001

8421 code is the predominant BCD code. So when refer to BCD, it


mean the 8421 code unless otherwise stated.

32
Decimal to BCD Conversion
Replace each decimal digit with the appropriate 4-bit code.

Determine the decimal numbers for the following BCD codes:


(a) 2910 (b) 44710

Solution: (a) 2 9

0010 1001 = 00101001BCD

(b) 4 4 7

0100 0100 0111 = 010001000111BCD


33
BCD to Decimal Conversion
Break the binary number into 4-bit groups, starting the right-most bit
and replace 4-bit group with the equivalent BCD codes.

Convert the following BCD codes to decimal:


(a) 01111001 (b) 100110000001

Solution: (a) 0111 1001

7 9 = 7910

(b) 1001 1000 0001

9 8 1 = 98110

34
BCD Addition

BCD is a numerical code and can be used in arithmetic operations.


Addition is the most important operation.
How to add two BCD numbers:
Step 1: Add the two BCD numbers, using the rules for binary addition.

Step 2: If a 4-bit sum is equal to or less than 9, it is a valid BCD number.

Step 3: If a 4-bit sum is greater than 9, or if carry out of 4-bit group is


generated, it is invalid result.

Add 6 (0110) to the 4-bit sum in order to skip the six invalid states
and return the code to 8421.

If a carry results when six is added, simply add the carry to the
next 4-bit group.
35
Example: Add the following BCD numbers

(a) 0010 2 (c) 1001 9


+ 0101 + 5 + 0011 + 3 .
0111 7 1100 12 (Invalid BCD number (>9))
. + 0110 Add 6
0001 0010 (Valid BCD number)
(b) 0100 0000 40
+ 0101 1000 58 1 2
1001 1000 98

(d) 1000 8
+1001 . 9 +
1 0001 17 (invalid because of carry)
. + 0110 Add 6
0001 0111 (Valid BCD number)

1 7 36
0110 0111 67
+ 0101 0011 + 53
1011 1010 (Both group are invalid >9) 120
+ 0110 +0110 Add 6 to both groups
0001 0010 0000

1 2 0

37
BCD 84-2-1 Code

The designation 84-2-1 indicates the binary weights of four bits (23, 22,-21,-20).
The weight for MSB is 8 and 1 for LSB.
The decimal to BCD 84-2-1conversion is shown in table below.

Decimal Digit 0 1 2 3 4 5 6 7 8 9

BCD 84-2-1 0000 0111 0110 0101 0100 1011 1010 1001 1000 1111

38
Decimal to BCD 84-2-1 Conversion
Replace each decimal digit with the appropriate 4-bit code.

Determine the decimal numbers for the following BCD codes:


(a) 1910 (b) 32710

Solution: (a) 1 9

0111 1111 = 01111111BCD84-2-1

(b) 3 2 7

0101 0110 1001 = 010101101001BCD84-2-1


39
BCD 84-2-1 to Decimal Conversion
Break the binary number into 4-bit groups, starting the right-most bit
and replace 4-bit group with the equivalent BCD codes.

Convert the following BCD codes to decimal:


(a) 11110110 (b) 010110011011

Solution: (a) 1111 0110

9 2 = 9210

(b) 0101 1001 1011

3 7 5 = 375 10

40
EXCESS-3
Excess-3 means that every decimal digit, 0 through 9, must be add with 3
and it is represented by a binary code of four bits.
The decimal to BCD conversion is shown in table below.

Decimal Digit 0 1 2 3 4 5 6 7 8 9

Excess-3 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100

Invalid number for excess-3 is 0000, 0001, 0010, 1101, 1110 & 1111.

41
Decimal to Excess-3 Conversion
Replace each decimal digit with the appropriate 4-bit code.

Determine the decimal numbers for the following BCD codes:


(a) 5610 (b) 28410

Solution: (a) 5 6

1000 1001 = 10001001xs-3

(b) 2 8 4

0101 1011 0111 = 010110110111xs-3


42
Excess-3 to Decimal Conversion

Break the binary number into 4-bit groups, starting the right-most bit
and replace 4-bit group with the equivalent excess-3 codes.

Convert the following BCD codes to decimal:


(a) 11000110 (b) 010110011011

Solution: (a) 1100 0110

9 3 = 9310

(b) 0100 0011 1011

1 0 8 = 10810

43
The GRAY code
Is unweighted and is not an arithmetic code.
There are no specific weights assigned to the bit positions.
It exhibits only a single bit change from one code word to the next in sequence.
Table 2-6 is a listing of the 4-bit Gray code for decimal 0 through 15.

Decimal Binary Gray Code Decimal Binary Gray Code

0 0000 0000 8 1000 1100


1 0001 0001 9 1001 1101
2 0010 0011 10 1010 1111
3 0011 0010 11 1011 1110
4 0100 0110 12 1100 1010
5 0101 0111 13 1101 1011
6 0110 0101 14 1110 1001
7 0111 0100 15 1111 1000
44
Binary to Gray Code Conversion
Rules:
The MSB (left-most) in the Gray code is same as the corresponding MSB
in binary numbers.
Going from left to right, add each adjacent pair of binary code bits to get
the next Gray code bit. Discard carries.

Example 1: 10011 to Gray code

1 0 0 1 1 Binary

1 1 0 1 0 Gray

Example 2: 0111 to Gray

0 1 1 1 Binary

0 1 0 0 Gray
45
Gray Code to Binary Conversion
Rules:
The MSB (left-most) in the binary code is same as the corresponding bit in
the Gray Code.
Add each binary code bits generated to the Gray code bit in the next
adjacent position. Discard carries.

Example 1: 10111 to binary


1 0 1 1 1 Gray

1 1 0 1 0 Binary

Example 2: 0111 to binary

0 1 1 1 Gray

0 1 0 1 Binary

46
Alphanumeric Codes
Codes that represent numbers and alphabetic characters
(letters).

Also represent other characters such as symbols and various


instructions.

At a minimum, it must represent 10 decimal digit and 26 letters of


the alphabet = 36 items.

So this number requires six bits in each code combination


because 5-bit is insufficient (25 =32).

26 = 64, so there are 28 unused code combinations which can be


used for other symbols.

The most common alphanumeric code is ASCII. 47


ASCII

Is the abbreviation for American Standard Code for Information Interchange.


Pronounced askee .
Also known as ANSI X3.4.
Is universally accepted alphanumeric code used in most computers and other
electronic equipment.

Have a 128 characters and symbols represented by 7-bit binary code.

Actually, ASCII is an 8-bit code with the MSB always 0.

This 8-bit code is 00 through 7F in hexadecimal.

The first 32 characters are nongraphic commands; used only for control purposes.
The other characters are graphic symbols include digit and alphabetic.
Table 2-7 is a listing of the ASCII code.
48
Example ASCII
Determine the binary ASCII code for the following statement;
(8 Ramadhan 1423H) ;

Solution:
Symbol Binary (ASCII) Symbol Binary (ASCII)

( 0101000 Space 0100000


8 0111000 1 0110001
Space 0100000 4 0110100
R 1010010 2 0110010
a 1100001 3 0110011
m 1101101 H 1001000
a 1100001 ) 0101001
d 1100100 Space 0100000
h 1101000 ; 0111011
a 1100001
n 1101110
Space 0100000

49
Extended ASCII Characters

Use ASCII values of 128 and above for extended character sets.

Represented by an 8-bit code series from 80 to FF in hexadecimal.

Contain characters in the following general categories:


- Foreign (non English) alphabetic characters
- Foreign currency symbols
- Greek letters
- Mathematical symbols
- Drawing characters
- Bar graphing characters
- Shading characters

Table 2-9 is list of the extended ASCII character set.

50
EBCDIC
Is the abbreviation for Extended Binary Coded Decimal Interchange Code.

Is a character encoding set used by IBM mainframes.

Used the full 8 bits, so parity checking cannot be used.

Has a wider range of control characters than ASCII.

There are four main blocks in the EBCDIC code page:

0000 0000 to 0011 1111 is reserved for control characters;


0100 0000 to 0111 1111 are for punctuation;
1000 0000 to 1011 1111 for lowercase characters
1100 0000 to 1111 1111 for uppercase characters and numbers.

51
Parity Method for Error
Detection

52
Parity Bit
Parity bit means for bit error detection.

Parity bit is attached to a group of bits to make the total number


of 1s in a group always even or always odd.

Even Parity makes the total number of 1s even.

Odd Parity makes the total number of 1s odd.

Parity bit can be attached to the code at either the beginning or


the end.

How parity bits are attached to a code??? See table 2-10.

53
Table 2-10: Parity bit for BCD number
EVEN PARITY ODD PARITY
P BCD P BCD
0 0000 1 0000
1 0001 0 0001
1 0010 0 0010
0 0011 1 0011
1 0100 0 0100
0 0101 1 0101
0 0110 1 0110
1 0111 0 0111
1 1000 0 1000
0 1001 1 1001
54
Detecting An Error
Parity bit provides for the detection of a single bit error in group.

Example:

BCD 1001 with odd parity = 11001

Parity BCD

Error occur: 11000


Bit error Error can be detected.

Error occur: 11111


2-bit error Error not be detected.

55

You might also like