0% found this document useful (0 votes)
7 views

Complements

Uploaded by

qzgh986
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Complements

Uploaded by

qzgh986
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Digital Logic Design

Lecture 3

Complements, Number Codes and Registers


Overvie
w
° Complement of numbers
• Addition and subtraction

° Binary coded decimal


° Gray codes for binary numbers
° ASCII characters
° Moving towards hardware
• Storing data
• Processing data
Complements

• In general, we (human beings) express negative numbers by placing


a minus (-) sign at the left end of the number.

• Similarly while representing the integers in binary format, we can


leave the left-most bit be the sign bit. If the left-most bit is a zero,
the integer is positive; if it is a one, it is negative.
• The top-most bit should tell us the sign of the integer.
• The negative of a negative integer is the original integer ie., -
(-55) is 55.
• x - y should give the same result as x + (-y). That is, 8 - 3
should give us the same result as 8 + (-3).
Complement

Take the first bit as a sign indicator, thus leaving the 7 last bits
to represent the numbers.

°This simple way to represent negative numbers has for it the


equally simple way to compute the negative of a given
number: just invert the first bit!. Hence, the number 3, for
example, represented as (00000011) will give (10000011) for -
3 (we have just toggled the first bit).
° This simple way has a drawback: the negative of 0
(00000000) is now (10000000), known as -0
Two’s Complement
Representation
• The two’s complement of a binary number
involves inverting all bits and adding 1.
• 2’s comp of 00110011 is 11001101
• 2’s comp of 10101010 is 01010110
• Called radix complement by Mano since 2’s
complement for base (radix 2).
• To find negative of 2’s complement number
take the 2’s complement.

000011002 = 1210 111101002 = -1210

Sign bit Magnitude Sign bit Magnitude


Two’s Complement
Shortcuts
° Algorithm 1 – Simply complement each bit and
then add 1 to the result.
• Finding the 2’s complement of (01100101)2 and of its 2’s
complement…

N = 01100101 [N] = 10011011

10011010 01100100

+ 1 + 1

--------------- ---------------

10011011 01100101
° Algorithm 2 – Starting with the least significant bit, copy all
of the bits up to and including the first 1 bit and then
complementing the remaining bits.
• N =01100101
[N] =10011011
1’s Complement
Addition
° Using 1’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (1)10 = +(0001)2 = 000012 in 1’s comp. 0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Add carry to low-order bit 0 0 1 1 0 1
Add carry 0
--------------
Final
Result
0 1 1 0 1
1’s Complement
Subtraction
° Using 1’s complement numbers, subtracting
numbers is also easy.
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10. - 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 1’s comp. --------------
• (-1)10 = -(0001)2 = 111102 in 1’s comp.
1’s comp
0 1 1 0 0
Step 1: Take 1’s complement of 2nd operand Add + 1 1 1 1 0
Step 2: Add binary numbers
Step 3: Add carry to low order bit
--------------
1 0 1 0 1 0
Add carry 1
Final
--------------
Result 0 1 0 1 1
2’s Complement
Addition
° Using 2’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (1)10 = +(0001)2 = 000012 in 2’s comp. 0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Ignore carry bit Final 0 0 1 1 0 1
Result

Ignore
2’s Complement
Subtraction
° Using 2’s complement numbers, follow steps for
subtraction
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10. - 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 2’s comp. --------------
• (-1)10 = -(0001)2 = 111112 in 2’s comp.
2’s comp
0 1 1 0 0
Step 1: Take 2’s complement of 2nd operand Add + 1 1 1 1 1
Step 2: Add binary numbers
Step 3: Ignore carry bit
--------------
Final
Result 1 0 1 0 1 1

Ignore
Carry
2’s Complement Subtraction: Example
#2

° Let’s compute (13)10 – (5)10.


• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2

° Adding these two 5-bit codes…


0 1 1 0 1
carry + 1 1 0 1 1
--------------
1 0 1 0 0 0

° Discarding the carry bit, the sign bit is seen to be


zero, indicating a correct result. Indeed,
(01000)2 = +(1000)2 = +(8)10.
2’s Complement Subtraction: Example
#3
° Let’s compute (5)10 – (12)10.
• (-12)10 = -(1100)2 = (10100)2
• (5)10 = +(0101)2 = (00101)2

° Adding these two 5-bit codes…


0 0 1 0 1
+ 1 0 1 0 0
--------------
1 1 0 0 1

° Here, there is no carry bit and the sign bit is 1.


This indicates a negative result, which is what we
expect. (11001)2 = -(7)10.
2’s Complement
Subtraction

° Let’s compute (13)10 - (5)10.


• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2

° Adding these two 5-bit codes…


0 1 1 0 1
carry + 1 1 0 1 1
--------------
1 0 1 0 0 0

° Discarding the carry bit, the sign bit is seen to be


zero, indicating a correct result.
Different Coding Schemes to Represent Data in Computer

Computer works only with binary numbers. It stores all types


of data in the form binary digits. The data is converted to
binary form before it is stored inside the computer the process
of converting data into binary form is known encoding. Data
can be converted into binary form by using different coding
schemes.
Data Representation and Communication
° Conversion to computer-usable representation using data
formats
Define the different ways human data may be represented, stored and
processed by a computer
Data Representation and Communication

° Numbers are important to computers as they


• Represent information precisely
• Can be processed

° Decimal numbers are favored by humans. Binary


numbers are natural to computers. Hence,
conversion is required.
° If little calculation is required, we can use some
coding schemes to store decimal numbers, for data
transmission purposes.
° Examples: BCD (or 8421), Excess-3, 8-4-2-1, 2421,
etc.
° Each decimal digit is represented as a 4-bit code.
° The number of digits in a code is also called the
length of the code.
BCD
° Binary Coded Decimal (BCD) represents each decimal digit
with four bits
• Ex. 0011 0010 1001 = 32910

° This is NOT the same as 001100101001 2


° Why do this? Because people think in decimal.
Digit Bit pattern
Note: the following 6 bit 0 0000
patterns are not used: 1 0001
1010 2 0010
1011
3 0011
1100
1101 4 0100
1110 5 0101
1111
6 0110
7 0111
8 1000
9 1001
Putting It All
Together

° BCD not very efficient


° Used in early
computers (40s, 50s)
ASCII Code

ASCII stands for American standard code for information


interchange.

It was published in 1968 by ANSI (American National


Standard Institute).

It is the most widely used coding scheme for personal


computers.
ASCII Code

° American Standard Code for Information


Interchange
° ASCII is a 7-bit code, frequently used with an 8th bit
for error detection (more about that in a bit).
Character ASCII (bin) ASCII (hex) Decimal Octal
A 1000001 41 65 101
B 1000010 42 66 102
C 1000011 43 67 103

Z
a

1

ASCII Code

000 001 010 011 100 101 110 111


0000 NULL DLE 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EDT DC4 Most
$ significant
4 bit D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011Least significant
VT ESC
bit + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ 24 o DEL
ASCII Code

e.g., ‘a’ = 1100001

000 001 010 011 100 101 110 111


0000 NULL DLE 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EDT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ 25 o DEL
ASCII Code

Alphabetic codes

000 001 010 011 100 101 110 111


0000 NULL DLE 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EDT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ 26 o DEL
ASCII Code

Numeric codes

000 001 010 011 100 101 110 111


0000 NULL DLE 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EDT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ 27 o DEL
ASCII Code

Punctuation, etc.

000 001 010 011 100 101 110 111


0000 NULL DLE 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EDT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ o DEL
28
Unicode

° Unicode stands for Universal code.


° It is a 16-bit code. It can represent 65536 characters or
symbols.
° It is developed by following the ASCII coding scheme.
° The first 256 codes in Unicode are identical to the 256 codes
used by ASCII system.
What is an ERROR

Error is a condition when the output information does not


match with the input information.

During transmission, digital signals suffer from noise that can


introduce errors in the binary bits travelling from one system
to other. That means a 0 bit may change to 1 or a 1 bit may
change to 0.
Error-Detecting codes

Whenever a message is transmitted, it may get scrambled


by noise or data may get corrupted.

To avoid this, we use error-detecting codes which are


additional data added to a given digital message to help us
detect if an error occurred during transmission of the
message.

A simple example of error-detecting code is parity check.


Error-Correcting codes

Along with error-detecting code, we can also pass some data


to figure out the original message from the corrupt message
that we received. This type of code is called an error-
correcting code.
How to Detect and Correct Errors?

To detect and correct the errors, additional bits are added
to the data bits at the time of transmission.

The additional bits are called parity bits. They allow


detection or correction of the errors.

The data bits along with the parity bits form a code word.
° Even parity -- Even parity means the number of 1's in the
given word including the parity bit should be even
(2,4,6,....).
° Odd parity -- Odd parity means the number of 1's in the
given word including the parity bit should be odd (1,3,5,....).
Pari
ty
°Cod
esParity codes are formed by concatenating a parity
bit, P to each code word of C.
° In an odd-parity code, the parity bit is specified so
that the total number of ones is odd.
° In an even-parity code, the parity bit is specified
so that the total number of ones is even.

P Information Bits
1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1
 
Added even parity bit Added odd parity bit
Pari
ty
°Cod
e Concatenate a parity bit to the ASCII code for the
characters 0, X, and = to produce both odd-parity
Exa
and even-parity codes.
mpl
e
Character ASCII Odd-Parity Even-Parity
ASCII ASCII

0 0110000 10110000 00110000

X 1011000 01011000 11011000

= 0111100 10111100 00111100


Binary Data Storage

• Binary cells store individual bits of data


• Multiple cells form a register.
• Data in registers can indicate different values
• Hex (decimal)
• BCD
• ASCII

0 0 1 0 1 0 1 1

Binary Cell
Register Transfer

° Data can move from register to register.


° Digital logic used to process data
° We will learn to design this logic

Register A Register B

Digital Logic
Circuits

Register C
Transfer of
Information

° Data input at keyboard


° Shifted into place
° Stored in memory

NOTE: Data input in ASCII


Building a
Computer
° We need processing
° We need storage
° We need communication

° You will learn to use and


design these components.
Summary
° 2’s complement most important (only 1
representation for zero).
° Important to understand treatment of sign bit for 1’s
and 2’s complement.
° Although 2’s complement most important, other
number codes exist
° ASCII code used to represent characters (including
those on the keyboard)
° Registers store binary data

You might also like