Lecture 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Number Systems and Data Representations

Signed Numbers

1
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
In real-life, both positive and negative numbers may be
used in digital computers

In ordinary use, a minus (-) symbol placed in front of the


magnitude designates a negative number and a plus (+)
symbol designates a positive number.
Digital computers use 1s and 0s to represent any type of
information. By convention , a “0” is for positive
numbers and a “1” for negative numbers

2
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
An n-bit signed binary integer consists of two parts, as
shown in the following layout.

3
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
The four commonly used signed integer schemes are:
1. Signed-magnitude representation
2. Signed one’s complement representation
3. Signed two’s complement representation
4. Offset or biased representation
This section presents the first three schemes.

The fourth one will be considered when we present


floating point numbers

4
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
For positive numbers, the representation is exactly the
same in the first three notations:
nS = 0
nM = true or absolute magnitude
For example, using an 8-bit format (field) the
representation of +26 is as follows

5
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Signed-Magnitude Representation
In a signed-magnitude notation, the leftmost bit represents
the sign while the remaining bits contain the absolute (true)
magnitude of the number

Table 1 below shows the representation of decimals +27, -11,


+0 and -0 in signed-magnitude notation using 8-bit format.

6
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Ranges of Signed Numbers in Signed-Magnitude Notation
Using an 8-bit format, the largest positive number and the smallest
negative number are:
The largest positive number is: 27-1 = +127
The smallest negative number is: -(27-1) = -127
For an n-bit format, the range of the numbers that can be
represented is
-(2n-1-1) (N)SM +(2n-1-1)
Although easy for people to understand, the signed-magnitude
notation has two major drawbacks:
1. There are two possible representations of zero and equal
2. It is hard to implement in logic
7
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Signed 1’s Complement Representation
In signed 1’s complement notation, the magnitude of the negative
number is expressed in the 1’s complement.
Table below shows the representation of decimals +27, -11,
+0 and -0 in signed 1’s complement using an 8-bit format.

For an n-bit format, the range of the numbers that can be represented is
-(2n-1-1) (N)S1’sC +(2n-1-1)
Again, this notation has two representations for the zero.
8
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Signed 2’s Complement Representation
In signed 2’s complement notation, the magnitude of the
negative number is expressed in the 2’s complement.
Table below shows the representation of decimals +27, -11,
+0 and -0 in signed 2’s complement using an 8-bit format.

For an n-bit format, the range of the numbers that can be


represented is -(2n-1) (N)S2’sC +(2n-1-1)
This notation has one representation for the zero.
9
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Signed 2’s Complement Representation…
When n= 8, the largest positive number and the smallest
negative numbers are
1. The largest positive number is: 27-1 = +127
2. The smallest negative number is: -(27) = -128

Although asymmetric, the signed 2’s complement has


ONE representation for the zero.
This makes it the most attractive and the mostly used
scheme for the representation of signed numbers in
digital computers.

10
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Comparing the Three Representations
Table below lists all signed numbers represented in a 3-bit
format using the three schemes.
We notice that the signed 2’s complement represents one
extra number compared to the two other notations

11
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Special Case in 2’s Complement
Whenever a signed number has a 1 in the sign bit and all
0s for the magnitude bits, its decimal equivalent is -2n-1,
where n is the length of the format. For example,

1000 = -24-1 = -23 = -8


10000000 = -28-1 = -27 = -128

12
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Summary
For an n-bit field, the ranges are:
For unsigned numbers: [0 .. (2n -1)]
For signed-magnitude representation
-(2n-1 -1) NSM +(2n-1 -1)
For signed 1’s complement representation
-(2n-1 -1) NS1’C +(2n-1 -1)
For signed 2’s complement representation
-(2n-1) NS2’C +(2n-1 -1)
13
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Finding Signed 2’s Complement
The rules to convert a negative decimal number to the
signed 2’s complement
We proceed as follows:
1. Convert the given decimal number to a positive binary
number
2. Take the 2’s complement of the number obtained in
step 1

Example: Using an 8-bit format, perform the following


conversions
14
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Finding Signed 2’s Complement…
Example: Using an 8-bit format, perform the following
conversions
-45
-100
-5
-130
Solution

15
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Two’s Complement Binary to Decimal
The rules to convert a signed 2’s complement to decimal,
1. Check the sign bit (MSB). If “0”, the number is
positive. Convert it to decimal.
2. If sign bit is “1”, the number is a signed 2’s
complement negative number. To convert it, perform
the following:
Take the 2’s complement of the negative binary
number
Convert the resulting positive number to
decimal and add a (-) sign in front
16
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Example: The following numbers are expressed in the signed 2’s
complement representation using 8-bit format. Determine the decimal
equivalent value.
i- 11010001 ii- 01010101 iii- 11001100
Solution
i- 11010001
The sign bit is “1”, the number is negative.
11010001 2’s C = 00101111 the number is – (47)10
ii- 01010101
The sign bit is “0”, the number is positive.
01010101 the number is + (85)10
iii- 11001100
The sign bit is “1”, the number is negative.
11001100 2’s C = 00110100 the number is – (52)10
17
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Two’s Complement Binary to Decimal
Alternative Approach
Consider an n-bit binary number expressed in the signed 2’s
complement notation: (N)2 = (an-1 an-2 an-3 .… a0)2
1. If (N)2 is positive, the MSB = “0”. The remaining bits
an-2 an-3 .… a0 form the true magnitude of (N)2.

The magnitude (N)2 is

18
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Two’s Complement Binary to Decimal
Alternative Approach…
2. If MSB = “1”, (N)2 is negative.
The remaining bits represent the magnitude of the number in
2’s complement notation. The decimal equivalent of this
negative number is obtained as follows

19
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
Example
The following numbers are expressed in the signed 2’s
complement representation using 8-bit format. Determine the
decimal equivalent value using the alternative approach.
i- 11010001 ii- 01010101 iii- 11001100
Solution
i- 11010001
The sign bit is “1”, the number is negative.
The decimal equivalent is:
(-1)27 + (1x26 + 0 + 1x24 + 0 + 0 + 0 + 1x20) =
-128 + (64 + 0 + 16 + 0 + 0 + 0 + 1) = -128 + 81 = - (47)10
20
Number Systems and Data Representations Dr. A. Benzekri
Signed Binary Numbers
ii- 01010101
The sign bit is “0”, the number is positive.
The decimal equivalent is:
0x27 + 1x26 + 0 + 1x24 + 0 + 1x22 + 0 + 1x20 =
0 + 64 + 0 + 16 + 0 + 4 + 0 + 1 = (47)10 = + (85)10

i- 11001100
The sign bit is “1”, the number is negative.
The decimal equivalent is:
(-1)27 + (1x26 + 0 + 0 + 1x23 + 1x22 + 0 + 0) =
-128 + (64 + 0 + 0 + 8 + 4 + 0 + 0) = -128 + 76 = - (52)10
21
Number Systems and Data Representations Dr. A. Benzekri
Subtraction of Unsigned
Binary Numbers

22
Number Systems and Data Representations Dr. A. Benzekri
Subtraction

Addition of binary numbers is similar to the decimal


addition we learned in primary school.
Subtraction is performed using complements.

23
Number Systems and Data Representations Dr. A. Benzekri
Subtraction
Subtraction Using 2's Complement
Consider two n-bit unsigned binary integers (M)2 and (N)2 .
The difference (D)2 = (M)2 – (N)2 is carried as follows:
Algorithm:
1. Add (M)2 to [(N)2]2
2. Check for carry 2n
If a carry is generated ( 2n = 1 ), discard it.
If no carry is generated (2n = 0 ), take the 2's
complement of the sum obtained in setp-1 and
place a negative sign in front of it.
24
Number Systems and Data Representations Dr. A. Benzekri
Subtraction
Proof
(D)2 = (M)2 – (N)2 = (M)2 - (N)2 + 2n - 2n
= (M)2 - (N)2 + 2n - 2n = (M)2 + 2n - (N)2 - 2n
= (M)2 + [(N)2]2 - 2n
a- Case (M)2 (N)2 where the difference is positive ((D)2 0)
(D)2 = (M)2 – (N)2 = (M)2 + [(N)2]2 - 2n
b- Case M2 < N2 where the difference is negative ((D)2 < 0 )
(D)2 = (M)2 – (N)2 = - (2n - ((M)2 + [(N)2]2 ) )
= - 2's complement of the sum
(D)2 = - ( 2n - 2n - (M)2 + (N)2) = - (2n - (2n + (M)2 – (N)2 ))
(D)2 = - ( 2n - ((M)2 + [(N)2]2 ))
25
Number Systems and Data Representations Dr. A. Benzekri
Subtraction
Example 1
Using 2’s complement, perform the following subtractions
(110111)2 - (101101)2
(101101)2 - (110111)2
Solution
a- 110111 ------------ 110111
101101 --2’sC---- 010011
1 001010
A carry out is “1” the difference is positive, result is: (001010)2
b- 101101 ------------ 101101
110111 --2’sC---- 110111
0 110110
Find 2’s C 001010
The carry out is “0”, the difference is negative. Taking the 2’s
complement of the sum yields: - (001010)2
26
Number Systems and Data Representations Dr. A. Benzekri
Subtraction
Example 2
Using 2’s complement, perform the following subtractions
(110110)2 - (10110)2
Solution
The two operands have different dimensions.
We first need to equalize the number of bits of both operands by
padding the subtrahend with a “0”.
We then take its 2’s complement before adding it with the minuend.

A carry out is generated, the difference is positive. Ignoring the carry,


the result is: (100000)2
27
Number Systems and Data Representations Dr. A. Benzekri
Subtraction
Example 3
Using 2’s complement, perform the following subtractions on mixed
numbers
(1010.11)2 - (1001.01)2
Solution

A carry out is generated, the difference is positive. Ignoring the carry,


the result is:
(0001.10)2 = (1.5)10

28
Number Systems and Data Representations Dr. A. Benzekri
Attention
Sorry, due to the lack of time
and the complexity of the topic,
I will not post lectures on
Floating Point Numbers.

Hope to do that face-to-face

29
Number Systems and Data Representations Dr. A. Benzekri

You might also like