You are on page 1of 48

DIGITAL LOGIC DESIGN

(DLD)

Lecture # 3

Compliments & Binary Codes


University of Management & Technology

SIGNED BINARY NUMBERS

Every Number can have both positive and


negative number. In binary number system
positive number can easily expressed.

A 0 is for positive, a 1 is for negative.

Take 4-bit, Normally 0 as a last bit represent


positive number (i.e. +5 = 0101)

Make last bit 1 for negative number (i.e. -5=1101)


2

SIGNED BINARY NUMBERS

It is usual to represent the sign with a bit placed in


the leftmost position of the binary number.

Sign bit

The Most common notations are:

Signed-magnitude system.

Signed-complement system.

Sign
Signbit
bit 00 positive
positive
Sign
Signbit
bit 11 negative
negative

SIGN MAGNITUDE FORM

In the Sign Magnitude Form, a negative number


has

the

same

magnitude

bits

as

the

corresponding positive number, but the sign bit is


a 1 rather than a 0.

SIGNED MAGNITUDE SYSTEM


Example
Example11

01001
11001

+9
9

Example
Example22
3-bit binary pattern
Bit Pattern

000

001

010

011

100

101

110

111

Signed-Magnitude
Decimal Value

+0

+1

+2

+3

-0

-1

-2

-3
5

SIGNED COMPLEMENT SYSTEM

In this system, a negative number is indicated by


its complement.

The signed-complement system can use either


the 1s complement or the 2s complement
notations.

1S COMPLEMENT

The1'

complementof

abinary numberis

defined as the value obtained by inverting all the


bits in the binary representation of the number
(swapping 0's for 1's and vice-versa).

The ones' complement of the number then


behaves like the negative of the original number
in some arithmetic operations.
7

EXAMPLE

Assuming the representation of the number 9 in binary


with 8-bits, we have the following cases:

Unsigned 9 or +9 has a the same representation in both


signed-magnitude and signed-complement systems which
is: 00001001

-9 has the signed-magnitude representation: 10001001

-9 has the signed-1s complement representation: 11110110

-9 has the signed-2s complement representation: 11110111


8

2S COMPLEMENT

The2' complementof abinary numberis defined as


the value obtained by first taking 1s Complement of
that number, and then adding 1 in it.
+5 = 0101

2' complement
add 1 in it
2' complement

1010
+1
1011
9

SIGNED COMPLEMENT SYSTEM


The signed-complement conversion table of a 3-bit binary pattern is as
follows:

Bit Pattern

000

001

010

011

100

101

110

111

Signed 1s complement
decimal value

+0

+1

+2

+3

-3

-2

-1

-0

Signed 2s complement
decimal value

+0

+1

+2

+3

-4

-3

-2

-1

10

SIGNED COMPLEMENT SYSTEM


4-bit system

Positive values

Negative values

Value

Sign-andMagnitude

1s
Comp.

2s
Comp.

Value

Sign-andMagnitude

1s
Comp.

2s
Comp.

+7
+6
+5
+4
+3
+2
+1
+0

0111
0110
0101
0100
0011
0010
0001
0000

0111
0110
0101
0100
0011
0010
0001
0000

0111
0110
0101
0100
0011
0010
0001
0000

-0
-1
-2
-3
-4
-5
-6
-7
-8

1000
1001
1010
1011
1100
1101
1110
1111
-

1111
1110
1101
1100
1011
1010
1001
1000
-

1111
1110
1101
1100
1011
1010
1001
1000
11

COMPLEMENT OF FRACTIONS

We can extend the idea of complement on fractions.

Examples:

Negate 0101.01 in 1s-complement


Answer:1010.10

Negate 111000.101 in 1s-complement


Answer: 000111.010

Negate 0101.01 in 2s-complement


Answer: 1010.11

12

BINARY ADDITION -RULES


0+0=0
0+1=1
1+0=1
1 + 1 = 0 (10, carry of one to the next bit)

13

ADDITION

Take 4-bit values by adding 6 and 7.


11
carry
0110
6
0111
7
8421
1101
13
1101

14

BINARY SUBTRACTION- RULES


0-0=0
1-1=0
1-0=1
10- 1 = 1

15

BINARY SUBTRACTION
Have previously looked at the subtraction
operation. A quick review.
Just like subtraction in any other base

Minuend

Subtrahand
Difference

10110
- 10010
00100

And when a borrow is needed.

16

AND A FULL EXAMPLE

And more ripple -

17

BINARY MILTIPLICATIN-RULES
0*0=0
0*1=0
1*0=0
1*1=1

18

BINARY MILTIPLICATIN

19

BINARY DIVISION

20

BINARY CODES
THE 8421 BCD
THE EXCESS-3 CODE
SELF COMPLEMENTING CODES

21

BCD

People understand decimal system better

BCD makes it easy to replace a decimal number


with an individual binary code

Decimal 15 is BCD 0001 0101 in Binary it was


1111

Since most computers store data in eight-bit


bytes

Ignore 4 extra bits

One can store two digits per byte, called "packed" BCD

22

THE 8421 BCD

BCD stands for Binary-Coded Decimal.


A BCD number is a four-bit binary group that
represents one of the ten decimal digits 0 through 9
Decimal
Digit
BCD

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

ote: 1010, 1011, 1100, 1101, 1110, and 1111 are INVALID CODE!
23

B.C.D.
Example:
Example:

Decimal number 4926

8421 BCD coded number 0100 1001 0010 0110

24

Question
Convert the BCD coded number
1000 0111 0001 into decimal.

BCD Coded Number


Decimal Number

1000 0111 0001


8

1
25

BCD ADDITION

BCD is a numerical code and can be used in arithmetic


operations. Here is how to add two BCD numbers:
1.

Add the two BCD numbers, using the rules for basic binary addition.

2.

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

3.

If a 4-bit sum > 9, or if a carry out of the 4-bit group is generated it is


an invalid result. Add 6 (0110) to a 4-bit sum in order to skip the
invalid states and return the code to 8421. If a carry results when 6 is
added, simply add the carry to the next 4-bit group .
26

BCD ADDITION - EXAMPLES

4+5

4+8
+

Sum is greater than 1001


Add 0110 (6)
27

BCD ADDITION - EXAMPLES

184+576
1
+

184

576

760

28

THE EXCESS-3 CODE

Add 3 to each digit of decimal and convert to 4-bit


binary form

Decimal Binary +3 Excess-3


0
0000 0011 0011
1
0001 0011 0100
2
0010 0011 0101
3
0011 0011 0110
4
0100 0011 0111
5
0101 0011 1000
6
0110 0011 1001
7
0111 0011 1010
8
1000 0011 1011
9
1001 0011 1100

Sample Problem:

Decimal

Excess-3

0110 1000 1100


29

SELF COMPLEMENTING CODES

The codes representing the pair of complementary digits are also


complementary to each other.

Example: Excess-3 code


0:
1:
2:
3:
4:
5:
6:
7:
8:
9:

0011
0100
0101
0110
0111
1000
1001
1010
1011
1100

30

SOME WEIGHTED CODES

Weight codes Different position has different weight

8421 code

84-2-1

0101=0x8+1x4+0x2+1x1=5
0101=0x8+1x4+0x(-2)+1x(-1)=3

31

THE GRAY CODE


The output data of many physical systems are
quantities that are continuous. These data must be
converted into digital form before they are applied to
a digital system.
The advantage of the Gray code over the straight
binary number sequence is that only one bit in the
code group changes in going from one number to the
next.
For example, in going from 7 to 8, the Gray code
changes from 0100 to 1100. Only the first bit changes,
from a 0 to 1 ; the other three bits remain the same.
By contrast. with binary numbers the change from 7
32
to 8 will be from 0111 to 1000, which causes all four
bits to change values.

THE GRAY CODE

Unweighted Code
Only a single bit change from one code value to the next.
Example: 4-bit standard Gray code

Decimal
0
1
2
3
4
5
6
7

Binary
0000
0001
0010
0011
0100
0101
0110
0111

Gray Code
0000
0001
0011
0010
0110
0111
0101
0100

Decimal
8
9
10
11
12
13
14
15

Binary
1000
1001
1010
1011
1100
1101
1110
1111

Gray code
1100
1101
1111
1110
1010
1011
1001
1000
33

Table Non-weighted Codes


Decimal

Excess-3 code

Gray code

0011

0000

0100

0001

0101

0011

0110

0010

0111

0110

1000

0111

1001

0101

1010

0100

1011

1100

1100

1101

10
11
12

1111
Not
defined

1110
1010

13

1011

14

1001

15

1000

34

WARNING: CONVERSION OR
CODING
Do

NOT mix up conversion of a decimal

number to a binary number with coding a


decimal number with a BINARY CODE.
13
13

10

= 11012 (This is conversion)

0001|0011 (This is coding)

35

CHARACTER CODES - ASCII

Many applications require handling of not only


numbers but letters and special characters.

ASCII

American

Standard

Code

for

Information Interchange.

7 Bits to store 128 characters

In ASCII, every letter, number, and punctuation


symbol has a corresponding number, or ASCII
code.

36

ASCII TABLE

37

ERROR DETECTION

Errors can occur during data transmission.


They

should

be

detected,

so

that

re-

transmission can be requested.

With binary numbers, usually single-bit errors


occur.
Example:

0010 erroneously transmitted as

0011 or 0000 or 0110 or 1010.

38

EVEN PARITY
Even

parity set bit to make


number of 1s even
Examples
1000001 with even parity is
01000001
1000011 with even parity is
11000011
39

ODD PARITY
Similar

except make the number of

1s odd
Examples
1000001 with odd parity is
11000001
1000011 with odd parity is
01000011

40

EVEN/ODD PARITY (1)


Computers

can sometimes make errors


when they transmit data.
Even/odd parity:
is

basic method for detecting if an odd number


of bits has been switched by accident.

Odd

parity:

The

number of 1-bit must add up to an odd


number

Even

The

parity:

number of 1-bit must add up to an even


number

EVEN/ODD PARITY (2)


The computer knows which parity it is using
If it uses an even parity:
If the number of of 1-bit add up to an odd
number then it knows there was an error:
If it uses an odd:
If the number of of 1-bit add up to an even
number then it knows there was an error:
However, If an even number of 1-bit is flipped the
parity will still be the same. But an error occurs
The even/parity cant this detect this error:

EVEN/ODD PARITY (3)

It is useful when an odd number of 1-bits is


flipped.
Suppose we have an 7-bit binary word (7-digits).
If you need to change the parity you need to
add 1 (parity bit) to the binary word.
You now have 8 digit word.
However, the computer knows that the added
bit is a parity bit and therefore ignore it.

EXAMPLE (1)
Suppose you receive a binary bit word 0101 and
you know you are using an odd parity.
Is the binary word errored?
The answer is yes:

There are 2 1-bit, which is an even number


We are using an odd parity
So there must have an error.

PARITY BIT

single bit is appended to each data chunk

makes

the number of 1 bits even/odd

Example:

even parity

1000000(1)
1111101(0)
1001001(1)

Example:

odd parity

1000000(0)
1111101(1)
1001001(0)

PARITY CHECKING
Assume we are using even parity with 7-bit ASCII.
The letter V in 7-bit ASCII is encoded as 0110101.
How will the letter V be transmitted?

Because there are four 1s (an even number), parity is set to zero.
This would be transmitted as: 01101010.

If we are using an odd parity:

The letter V will be transmitted as 01101011

EXERCISE 1
Suppose you are using an odd parity. What
should the binary word 1010 look like after you
add the parity bit?
Answer:

There

is an even number of 1-bits.


So we need to add another 1-bit
Our new word will look like 10101.

EXERCISE 2
Suppose you are using an even parity. What
should the binary word 1010 look like after you
add a parity bit?
Answer:

There

is an even number of 1s.


So we need to add another 0
Our new word will look like 10100.

You might also like