23
Complements and Codes by Dr. Amin Danial Asham

Complements and Codes by Dr. Amin Danial Asham. References Digital Design 5 th Edition, Morris Mano Programmable Controllers-Theory and Implementation,

Embed Size (px)

Citation preview

Page 1: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

Complements and Codes

by

Dr. Amin Danial Asham

Page 2: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

References

Digital Design 5th Edition, Morris Mano

Programmable Controllers-Theory and Implementation, 2nd Edition, L.A. Bryan and E.A. Bryan

Page 3: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS

Complements are used in digital computers to simplify the

subtraction operations and for logical manipulations.

Simplifying operations leads to simpler, less expensive and hence

less expensive circuits to implement the operations.

There are to types of complement; radix r’s complement and

diminished radix (r-1)’s complement.

Therefore for binary system we have 2’s complement and 1’s

complement and similarly for decimal system we have 10’s

complement and 9’s complement.

The concept is applied to any number system.

Page 4: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS

1. Diminished Radix Complement

For any number N in base r having n digits (r-1)’s complement of N is defined

as .

For r=10 and r-1=9, so the 9’s complement of N is (, where n is the number of

digits of N.

For example:

The 9’s complement of 546700 is

The 9’s complement of 012398 is

As shown in the former numerical examples, the 9's complements of a decimal

number is obtained by subtracting each digit from 9.

Page 5: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS (continue)

1. Diminished Radix Complement (continue) For binary numbers r=2 and r-1 =1, so the 1’s complement of N is . For Example :

The 1’s complement of 1011000 is . The 1’s complement of 1011000 is .

From the former examples , the 1's complement of a binary number is obtained by subtracting each digit from 1.

However, when subtracting binary digits from 1, we can have either 1 - 0 = 1 or 1 - 1 = 0, which causes the bit to change from 0 to 1 or from 1 to 0, respectively.

Therefore, the 1's complement of a binary number is formed by changing 1's to 0's and 0's to 1's.

In a similar way, The (r - 1)'s complement of octal or hexadecimal numbers is obtained by subtracting each digit from 7 or F (decimal 15), respectively.

Page 6: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

III. COMPLEMENTS (continue)

2. Radix Complement The radix complement of n digit number N in base-r system is defined

as for and as 0 for . The radix complement of in base r system equals the 1 added to the

diminished radix complement of N in the same base. Since:

Therefore for any non-zero number N, the radix complement can be obtained by getting the diminished radix complement and then adding 1.

Alternatively, since equals a 1 followed by n zeros , therefore, , which is the 10’s complement of N , can be formed also by leaving all least significant 0's unchanged, subtracting the first nonzero least significant digit from 10, and subtracting all higher significant digit from 9.

The 10’s complement of 150 is 1000-150=850

Page 7: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

III. COMPLEMENTS (continue)

2. Radix Complement (continue) Examples for 10’s complement :The 10 ‘s complement of 012398 is 987602The 10 ‘s complement of 246700 is 753300

Similarly. The 2’s complement of n digit binary number N is which can be obtained by:

leaving all least significant 0's unchanged, and the first 1 unchanged

changing 1's to 0's and 0's to 1's in all higher significant digits

The 2’s complement of 1101100 is 0010100

Page 8: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

III. COMPLEMENTS (continue) Notes

• If the original number contains a radix point, the point should be removed temporary in order to obtain the r's or (r - 1)'s complement . The radix point is then restored to the complemented number in the same relative position.

• The complement of the complement gives the original value of the number.

Page 9: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

III. COMPLEMENTS (continue)

3. Subtraction with Complements

The subtraction operation , where M and N are n digits base r numbers, can be performed by adding M and the radix complement of N, therefore the result is

If , the sum will produce an end carry rn, Which can be discarded. what is left is M-N=72532 –3250=72532-03250=72532+96750

M-N=1010100-1000011=10110100+0111101

Page 10: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS (continue)

3. Subtraction with Complements (continue) If the addition does not produce an end carry and the result equals , which is the r’s complement of .

We recognize the negative numbers by the absence of end carry and the complemented result .

M-N=03250-72532 =03250+27468

Where 30718 is the 10’s complement of -69282

M-N=1000011-1010100=1000011+0101100

𝑊 h𝑒𝑟𝑒𝟏𝟏𝟎𝟏𝟏𝟏𝟏 𝑖𝑠𝑡 h𝑒2 ’ 𝑠𝑐𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑜𝑓 −𝟎𝟎𝟏𝟎𝟎𝟎𝟏

Page 11: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

Positive result

IV. COMPLEMENTS (continue)

3. Subtraction with Complements (continue) The diminished radix complement can be used to convert subtraction

to addition in a similar way of radix complement. Since the diminished radix complement is less by one than the radix

complement, therefore after discarding the end carry one must be added to get the sum to get the final result, which is called end-around carry.

Negative result

Page 12: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS (continue)

4. Signed Binary Numbers Unsigned numbers means we are dealing with non-negative values,

while signed number means we are dealing with positive and negative values.

We usually omit the sign symbol when we are dealing with unsigned number such as 9. On the other hand, signed numbers are preceded with a sign symbol such as +9, -6,…etc.

In computer system numbers are represented in binary form and the most left bit is used to indicate the sign in case of signed numbers For example, if we have a 4-bit register to store unsigned numbers,

therefore this register can store numbers from 00002 =010 to 11112 =1510.

On the other hand, to use this register to store signed numbers, the most left bit is used as indication for the sign and hence the register actually stores positive and negative values of 3 bit numbers since there are one bit reserved for the sign indication.

Page 13: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

IV. COMPLEMENTS (continue)

4. Signed Binary Numbers (continue) The negative numbers are stored in the form of 2’s complement by subtracting from . In the same way, for a 8-bit register (byte), it can store unsigned numbers from 010 =000000002 to

25510 =111111112,

and stores signed numbers from

010 =000000002 to 12710 =011111112

and -12810=100000002 to -110 =

111111112.

In general for any n-bit register, it

can store unsigned numbers from 0

up to 2n -1 or signed number from 0

to 2n-1 -1 and from -2n-1 to -1

in the form of 2’complement of n-bit

number .

Why 1’s complement is not commonly

used in arithmetic operations?

1’s complement imposes some

difficulties. For example there are two

representations of zero:

"positive" zero and a "negative" zero

Page 14: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES An important requirement of programmable controllers is communication with various

external devices that either supply information to the controller or receive information from the controller. This input/output function involves the transmission, manipulation, and storage of binary data that, at some point, must be interpreted by humans. Although machines can easily handle this binary data, we require that the data be converted to a more interpretable form.

One way of satisfying this requirement is to assign a unique combination of 1s and 0s to each number, letter, or symbol that must be represented. This technique is called binary coding.

In general, there are two categories of codes:a) those that represent numbers only b) and those that represent letters, symbols, and decimal numbers.

Several codes for representing numbers, symbols, and letters are standard throughout the industry. Among the most common are the following:

a) ASCIIb) BCDc) Gray

Page 15: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)a) ASCII

Alphanumeric codes (which use a combination of letters, symbols, and decimal numbers) are used when information processing equipment, such as printers and displays, must process the alphabet along with numbers and special symbols.These alphanumeric characters—26 letters (uppercase), 10 numerals (0-9), plus mathematical and punctuation symbols— can be represented using a 6-bit code (i.e., 26 = 64 possible characters).The most common code for alphanumeric representation is ASCII (the American Standard Code for Information Interchange).An ASCII (pronounced as-kee) code can be 6, 7, or 8 bits.Although a 6-bit code (64 possible characters) can accommodate the basic alphabet, numbers, and special symbols, standard ASCII character sets use a 7-bit code (27 = 128 possible characters), which provides room for lower case and control characters, in addition to the characters already mentioned.This 7-bit code provides all possible combinations of characters used when communicating with peripherals and interfaces.

Page 16: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)

a) ASCII (continue)An 8-bit ASCII code is used when parity check is added to a standard 7-bit code for error-checking purposes (note that all eight bits can still fit in one byte). shows the binary ASCII code representation of the letter Z (1328)

A parity bit is an extra to make the total number of 1's either even or odd. Consider the following two characters and their even and odd parity:

Page 17: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)

a) ASCII (continue) 7-bit ASCII Code

Page 18: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)

b) BCDThe binary coded decimal (BCD) system was introduced as a convenient way for humans to:

i. handle numbers that must be input to digital machines and

ii. interpret numbers that are output from machines. The best solution to this problem was to convert a code readily handled by man (decimal) to a code readily handled by processing equipment (binary). The result was BCD.

The decimal system uses the numbers 0 through 9 as its digits, whereas BCD represents each of these numbers as a 4-bit binary number.

The following table illustrates the relationship between the BCD code and the binary and decimal number systems.

Page 19: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)

b) BCD (continue)The BCD representation of a decimal number is obtained by replacing each decimal digit with its BCD equivalent. The BCD representation of decimal 7493 is shown here as an example:

Typical PLC applications of BCD codes include data entry (time, volume, weight, etc.)

via thumbwheel switches (TWS),

data display via seven-segment displays,

input from absolute encoders, and analog input/output instructions

Page 20: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

V. BINARY CODES(continue)

b) GRAYIt is basically a binary code that has been modified in such a way that only one bit changes as the counting number increases.

In standard binary, as many as four digits can change when counting with as few as four binary digits.

This drastic change is seen in

the transition from binary 7 to 8The Gray code is used in

applications in which the normal

sequence of binary numbers may

Produce an error or ambiguity

during the transition from one

number to the next. If binary numbers are used.

A change. for example from 0111

to 1000 may produce an

intermediate erroneous number

1001 if the value of the rightmost

bit takes longer to change than do

the values of the other three bits.

1-digits change

4-digits change

Page 21: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

VI. REGISTER WORD FORMATS

As previously mentioned, a digital computer performs all of its internal operations in binary format using 1s and 0s. Each binary piece of data is a bit. Eight bits make up one byte. Two bytes, or 16 bits, make up one word.A word is also called a register or location. Although the data stored in a register is represented by binary 1s and 0s, the format in which this binary data is stored may differ. Generally, data is represented in either straight (non-coded) binary or coded in a code such as binary coded decimal (BCD) format.

ByteWord(16-bit register)

Page 22: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

VI. REGISTER WORD FORMATS

1) BINARY FORMAT

Data stored in binary format can be directly converted to its decimal equivalent without any special restrictions.In this format, a 16-bit register can represent a maximum value of 6553510.

The value 6553510 in binary format all bits are 1.

Hence, 16-bits word may store the statuses of sixteen devices. If the most significant bit of the register is used as a sign bit, then the maximum decimal value that the 16-bit register can store is +3276710 or –3276710 in 2’s complement.

Page 23: Complements and Codes by Dr. Amin Danial Asham. References  Digital Design 5 th Edition, Morris Mano  Programmable Controllers-Theory and Implementation,

Thanks