Sei sulla pagina 1di 39

BINARY CODE (NEGATIVE REPRESENTATION)

by Lad Ronak eITRA

SIGNED BINARY NUMBERS

Representations for signed binary numbers:

1. Sign and Magnitude 2. 1's Complement 3. 2's Complement

SIGN-MAGNITUDE REPRESENTATION
In this representation, the leftmost bit of a binary code represents the sign of the value:

0 for positive, 1 for negative;

the remaining bits represent the numeric value.


3

1'S COMPLEMENT

A positive number, N, is represented in the same way as in the Sign and Magnitude representation. For an n-bit number,

The leftmost bit (sign bit) = 0.

Indicating a positive number.

The remaining n-1 bits represent the magnitude.

1'S COMPLEMENT

A negative number, -N, is represented by the 1's complement of the positive number, N. N' = 1's complement representation for -N. For an n-bit signed binary number,

The leftmost bit (sign bit) = 1 for all negative numbers in the 1's Complement system.

N' = (2n 1) N
5

1'S COMPLEMENT

The 1's Complement representation for -N can also be determined by taking the bit-wise complement of N. N' = 1's Complement representation for -N. For an n-bit signed binary number,

i.e. complement N, bit-by-bit

N' = bit-wise complement of N


6

1'S COMPLEMENT

To determine the magnitude of a negative number, -N, that is represented by its 1's Complement, N', simply take the 1's complement of the 1's Complement.

N = (2n 1) N'
positive #

1's complement rep. for negative #

or

N = bit-wise complement of N'


7

SIGN-MAGNITUDE REPRESENTATION
To Compute negative values using Sign/Magnitude (signmag) representation. Begin with the binary representation of the positive value, then flip the leftmost zero bit.

SIGN-MAGNITUDE REPRESENTATION
Ex 1. Find the signmag representation of -610 Step1: find binary representation using 8 bits 610 = 000001102 Step2: if the number is a negative number flip left most bit 10000110 So: -610 = 100001102 form) (in 8-bit sign/magnitude
9

SIGN-MAGNITUDE REPRESENTATION
Ex 2. Find the signmag representation of -3610 Step 1: find binary representation using 8 bits 3610 = 001001002 Step 2: if the number is a negative number flip left most bit 10100100 So: -3610 = 101001002 form) (in 8-bit sign/magnitude
10

SIGN-MAGNITUDE REPRESENTATION
Ex 3. Find the signmag representation of 7010 Step 1: find binary representation using 8 bits 7010 = 010001102 Step 2: if the number is a negative number flip left most bit 01000110 (no flipping, since it is +ve) So: 7010 = 010001102 (in 8-bit sign/magnitude form)

11

SIGN-MAGNITUDE REPRESENTATION

What is this signmag number?


100000002 The machine will think of it as - 0 , which is a non valid value.

12

TWOS COMPLEMENT REPRESENTATION


Another scheme to represent negative numbers The leftmost bit serves as a sign bit:

0 for positive numbers, 1 for negative numbers.

13

TWOS COMPLEMENT REPRESENTATION


To Compute negative values using twos Complement representation, begin with the binary representation of the positive value, complement (flip each bit if it is 0 make it 1 and visa versa) the entire positive number, and then add one.

14

TWOS COMPLEMENT REPRESENTATION


Ex.

Find the representation of


610

twos

complement

Step1: find binary representation in 8 bits 610 = 000001102

15

TWOS COMPLEMENT REPRESENTATION


Step 2: Complement the entire positive number, and then add one 00000110 11111001 1 11111010

(complemented) -> (add one) -> +

So: -610 = 111110102 (in 2's complement form, using any of above methods)

16

TWOS COMPLEMENT REPRESENTATION


Alternative method for step 2 Scan binary representation from right too left, find first one bit, from low-order (right) end, and complement the remaining pattern to the left.

(left complemented) -->

00000110 11111010
17

TWOS COMPLEMENT REPRESENTATION


Ex 2: Find the Twos Complement of -7610 Step 1: Find the 8 bit binary representation of the positive value. 7610 = 010011002

18

TWOS COMPLEMENT REPRESENTATION


Step 2: Find first one bit, from low-order (right) end, and complement the pattern to the left.

(left complemented)

->

01001100 10110100

So: -7610 = 101101002 (in 2's complement form, using any of above methods)
19

TWOS COMPLEMENT REPRESENTATION


Ex 3: Find the Twos Complement of 7210 Step 1: Find the 8 bit binary representation of the positive value. 7210 = 010010002

20

TWOS COMPLEMENT REPRESENTATION


Step 2: Since number is positive do nothing. So: 7210 = 010010002 (in 2's complement form, using any of above methods)

21

TWOS COMPLEMENT REPRESENTATION


The most important characteristic of the twoscomplement system is that the binary codes can be added and subtracted as if they were unsigned binary numbers, without regard to the signs of the numbers they actually represent.

22

TWOS COMPLEMENT REPRESENTATION


For example, to add +4 and -3, we simply add the corresponding binary codes, 0100 and 1101: 0100 (+4) +1101 (-3) 0001 (+1) A carry from the leftmost column has been ignored. The result, 0001, is the code for +1, the sum of +4 and -3.

23

TWOS COMPLEMENT REPRESENTATION


Likewise, to subtract +7 from +3, we add the code for -7, 1001, to that of +3, 0011: 0011 (+3) +1001 (-7) 1100 (-4)

The result, 1100, is the code for -4, the result of subtracting +7 from +3.

24

TWOS COMPLEMENT REPRESENTATION


Benefits of Twos Complements:

addition and subtraction simplified in the twoscomplement system, In 8 bits, -0 has been eliminated, replaced by -128, for which there is no corresponding positive number.

25

2'S COMPLEMENT

A positive number, N, is represented in the same way as in the Sign and Magnitude representation. For an n-bit number,

The leftmost bit (sign bit) = 0.

Indicating a positive number.

The remaining n-1 bits represent the magnitude.

26

2'S COMPLEMENT

A negative number, -N, is represented by the 2's complement of the positive number, N. N* = 2's complement representation for -N. For an n-bit signed binary number,

The leftmost bit (sign bit) = 1 for all negative numbers in the 2's Complement system. n

N* = (2 ) N

27

2'S COMPLEMENT

Thus, the 2's Complement representation for -N can also be determined by adding 1 to the 1's Complement representation for -N.

N' = 1's Complement representation for -N. N* = 2's Complement representation for -N.

For an n-bit signed binary number,

N* = N' + 1

28

2'S COMPLEMENT

To determine the magnitude of a negative number, -N, that is represented by its 2's Complement, N*, simply take the 2's complement of the 2's Complement.

N = (2n) N*
positive #

or

2's complement rep. for negative #

N = (N*)' + 1
bit-wise complement of 2's complement
29

SIGNED BINARY NUMBERS

30

QUESTIONS?

31

OVERFLOW

The general rule for detecting overflow when performing 2's Complement or 1's Complement Addition:

An overflow occurs when the addition of two positive numbers results in a negative number. An overflow occurs when the addition of two negative numbers results in a positive number. Overflow cannot occur when adding a positive number to a negative number.

32

BINARY CODES

Weighted Codes

Each position in the code has a specific weight Decimal value of code can be determined Positions of code do not have a specific weight Decimal value assigned to each code

Unweighted Codes

33

BINARY CODES

n-bit Weighted Codes

Code: an-1an-2an-3...a1a0 Weights: wn-1, wn-2, wn-3, ..., w1, w0 Decimal Value: an-1 x wn-1 + an-2 x wn-2 + + a1 x w1 + a0 x w0 Code: a3a2a1a0

4-bit Weighted Code

34

BINARY CODES

Examples of 4-bit weighted codes

8-4-2-1

4 bits 16 code words Only 10 code words required to represent decimal digits 4 bits 16 code words 4 bits 16 code words

6-3-1-1

Excess-3 (obtained from 8-4-2-1)

35

BINARY CODES

Examples of unweighted codes

2-out-of-5 Code

Exactly 2 of the 5 bits are 1 for a valid code word. 10 valid code words. Code values for successive decimal digits differ in exactly one bit. 4 bits 16 code words.

Gray Code

36

BINARY CODES
Decimal Digit 0 1 2 3 4 5 6 7 8 9 8-4-2-1 6-3-1-1 Code Code (BCD) 0000 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0001 0011 0100 0101 0111 1000 1001 1011 1100 Excess-3 2-out-of-5 Code Code 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 00011 00101 00110 01001 01010 01100 10001 10010 10100 11000 Gray Code 0000 0001 0011 0010 0110 1110 1010 1011 1001 1000
37

BINARY CODED DECIMAL (BCD)

4-bit binary number used to represent each decimal digit. Weighted code: 8-4-2-1 Binary values 0000 1001 used to represent decimal values 0 9. Binary values 1010 1111 not used. Very different from binary representation.

38

BINARY CODED DECIMAL

In BCD, each decimal digit is replaced by its binary equivalent value. Example:

Binary:

937.2510 = 1110101001.012

39

Potrebbero piacerti anche