3 Lecture Three

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

Lecture Three

By

Dr. Noor Abdul Khaleq Z.


12/11/2023
Content

❑ Signed Numbers.

❑ Arithmetic Operation with Signed Binary


Numbers.

❑Binary Coded Decimal (BCD).

❑ Digital codes.
Signed Numbers
❑ A signed binary number consists of sign and magnitude information.
❑ The sign indicates whether the number is positive or negative
❑ The magnitude is the value of the number.
❑ There are three forms in which signed integer (whole) numbers can be
represented in binary:
➢ sign-magnitude.
➢ 1’s complement.
➢ 2’s complement.

❑ The Sign Bit: The left-most bit in a signed binary number.


❑ A 0 sign bit indicates a positive number.
❑ A 1 sign bit indicates a negative number.
Signed Numbers
1. Sign-Magnitude Form

❑ When a signed binary number is represented in sign-magnitude, the left most


bit is the sign bit while the remaining bits are the magnitude bits.

❑ Example:
❑ The decimal number +25 is expressed as an 8-bit signed binary number using
the sign-magnitude form as
0 0011001
sign bit magnitude bits

❑ The decimal number -25 is expressed as

1 0011001
sign bit magnitude bits
Signed Numbers
2. 1’s Complement Form

❑ Positive numbers in the 1’s complement form are represented the same as in
the positive sign-magnitude form.
❑ Negative numbers are the 1’s complements of the corresponding positive
numbers.

❑ Example:
Using 8-bits, the decimal number -25 is expressed as the 1’s complement of +25

❑ 00011001 (+25)
❑ 11100110 (-25) using the 1’s complement form
Signed Numbers
3. 2’s Complement Form

❑ Positive numbers in the 2’s complement form are represented the same as in the
positive sign-magnitude form and the 1’s complement form.
❑ Negative numbers are the 2’s complements of the corresponding positive
number.

❑ Example:
Using 8-bits, the decimal number -25 is expressed as the 2’s complement of +25

❑ 00011001 (+25)
❑ 11100111 (-25) using the 2’s complement form
The Decimal Value of Signed Numbers
Sign-Magnitude
❑ Decimal values of positive and negative numbers in the sign-magnitude form
are determined by summing the weights in all the magnitude bit positions where
there are 1s.
❑ The sign is determined by examining the sign bit

❑ Example: Determine the decimal value of the signed binary number expressed
in sign-magnitude: 10010101

❑ Solution:
❑ The seven magnitude bits and their power of two weights are as follows:
26 25 24 23 22 21 20
0 0 1 0 1 0 1
Summing the weights where there are 1s
16 + 4+ 1 =21
The sign bit is 1: therefore, the decimal number is -21
The Decimal Value of Signed Numbers
1’s Complement
❑ Decimal values of positive numbers in the 1’s complement form are determined
by summing the weights in all the bit positions where there are 1s.
❑ Decimal values of negative numbers are determined by assigning a negative
value to the weight of the sign bit, summing all the weights where there are 1s,
and adding 1 to the result.
❑ Example: Determine the decimal values of the signed binary numbers
expressed in 1’s.
a) 00010111 b)11101000
❑ Solution:
a) The bits and their power of two weights for b) The bits and their power of two weights for
positive number as follows: negative number are as follows:
27 26 25 24 23 22 21 20 2-7 26 25 24 23 22 21 20
1 1 1 0 1 0 0 0
0 0 0 1 0 1 1 1
Summing the weights where there are 1s
Summing the weights where there are 1s
-128 + 64 + 32+ 8= -24
16 + 4+ 2+ 1 =23
The sign bit is 1: therefore, the decimal number is
The sign bit is --:
0 therefore, the decimal number
-24
is +23
Add 1 to the result
-24+1 = -23
The Decimal Value of Signed Numbers
2’s Complement
❑ Decimal values of positive and negative numbers in the 2’s complement form
are determined by summing the weights in all bit positions where there are 1s.
❑ The weight in the sign bit in a negative number is given a negative

❑ Example: Determine the decimal values of the signed binary numbers


expressed in 2’s complement
a) 01010110 b) 10101010
❑ Solution:

a) The bits and their power of two weights for b) The bits and their power of two weights for
positive number as follows: negative number are as follows:

27 26 25 24 23 22 21 20 2-7 26 25 24 23 22 21 20
1 0 1 0 1 0 1 0
0 1 0 1 0 1 1 0
Summing the weights where there are 1s
Summing the weights where there are 1s
64 +16 + 4+ 2 = +86 -128 + 32+ 8 +2= -86
❑ Why the 2’s Complement Form is preferred for representing signed
integer numbers over the 1’s Complement Form?

1- To convert to decimal, it only requires the summation of the weights


regardless whether the number is positive or negative, while in the 1’s
complement form, it requires adding 1 to the summation of weights for the
negative numbers only.

2- In the 1’s complement, there are two representation for zeros (00000000 or
11111111).
Range of Signed Integer Numbers
❑ The formula for finding the number of different combinations of n bits is
Total combinations = 2n
❑ For example for one byte (8 bits), you can represent 28 different
numbers, and for four bytes (32 bits), you can represent 4.295 ×109
different numbers.
❑ For 2’s Complement signed numbers, the range values for n-bit numbers
is:
Range = - (2n-1) to + (2n-1 – 1 )
❑ For example, with four bits you can represent numbers in 2’s
complement ranging from –(23) = -8 to 23 -1 = +7. Also, with eight bits
you can go from -128 to +127.
❑ Note: There is one less positive number than there are negative numbers
because zero is represented as a positive number (all zeros).
Arithmetic Operation with Signed Binary Numbers
❑ The Addition of two numbers in the signed magnitude system follows the
rules of ordinary arithmetic.
❑ If the signs are the same, we add the two magnitudes and give the sum of the
common sign. If the signs are different, we subtract the smaller magnitude
from the larger and give the difference the sign of the larger magnitude.
❑ This is a process that requires a comparison of the signs and magnitudes and
then performing either addition or subtraction.
❑ In contrast, the rule for adding numbers in the signed‐complement system in
2’s complement does not require a comparison or subtraction, but only
addition.
❑ The addition of two signed binary numbers with negative numbers represented
in signed‐2’s‐complement form is obtained from the addition of the two
numbers, including their sign bits. A carry out of the sign‐bit position is
discarded.
❑ If resultant sum is positive, you can find the magnitude of it directly. But, if the
resultant sum is negative, then take 2’s complement of it in order to get the
magnitude.
The Addition with Signed Binary Numbers

❑ Example: perform the addition of two binary numbers +7 (00111) and


+4 (00100) using signed magnitude system.
❑ Solution:
00111 (+7) + 00100 (+4) = 01011 (+11)

❑ Example: perform the addition of two binary numbers -7 (10111) and


+4 (00100) using signed magnitude system.
❑ Solution:
10111 (-7) + 00100 (+4) = 10011 (-3)
The Addition with Signed Binary Numbers

❑ Example: perform the addition of two binary numbers -7 (10111) and


-4 (10100) using 2’s complement method.
❑ Solution: Take the 2’s complement of the two negative binary numbers.
11001 (-7) + 11100 (-4) = 110101 (-11)
Note: The result sum contains 6 bits. In this case, carry is obtained from sign
bit. So, we can remove it.
Resultant sum after removing carry is (10101). The sign bit ‘1’ indicates that
the resulted sum is negative. So, by taking 2’s complement of it we will get
the magnitude of resultant sum as 11 in decimal.

Example: perform the addition of two binary numbers -7 (10111) and


+4 (00100) using 2’s complement method.
❑ Solution:
11001 (-7) + 00100 (+4) = 11101 (-3)
The sign bit ‘1’ indicates that the resulted sum is negative. And taking 2’s
complement of result sum as -3
Subtraction of Two Signed Binary Numbers

❑ Take the 2’s complement of the subtrahend (including the sign bit) and add it
to the minuend (including the sign bit). A carry out of the sign‐bit position is
discarded.
❑ This procedure is adopted because a subtraction operation can be changed to
an addition operation if the sign of the subtrahend is changed, as is
demonstrated by the following relationship:

(A) - (+B) = (A) + (-B);


(A) - (-B) = (A) + (+B).

❑ But changing a positive number to a negative number is easily done by taking


the 2’s complement of the positive number. The reverse is also true.
❑ This is the advantage of 2’s complement technique (computers need only one
common hardware circuit to handle both types of arithmetic).
Subtraction with Signed Binary Numbers

❑ Example: perform the subtraction of two binary numbers +7 (00111) and


+4 (00100).
❑ Solution:
00111 (+7) - 00100 (+4) 00111(+7) + 11100 (-4) = 100011(+3)
The sign bit ‘0’ indicates that the resultant sum is positive. So, the magnitude
of it is 3 in decimal number system.

❑ Example: perform the subtraction of two binary numbers +7 (00111) and


+4 (00100)
❑ Solution:
00100 (+4) - 00111 (+7) 00100 (+4) + 11001 (-7) = 11101(-3)
The sign bit ‘1’ indicates that the resultant sum is negative. So, by taking 2’s
complement of it we will get the magnitude of resultant sum as 3 in decimal
number system.
Binary Coded Decimal (BCD)
❑ Binary coded Decimal BCD means that each decimal digit, 0 through 9, is
represented by a binary code of four bits.
❑ We like to read and write in decimal. Therefore, the BCD code provides an
excellent interface to binary systems.

The 8421 BCD Code


❑ The 8421 is a type of BCD code.
❑ 8421 indicates the binary weights of the four bits (23 ,22 ,21 ,20).
❑ The main advantage of this code is the ease of the conversion between 8421
code number and the decimal numbers.
❑ When we refer to BCD, we always mean 8421 code unless otherwise stated

Decimal-to-BCD conversion
Decimal 0 1 2 3 4 5 6 7 8 9
Digit
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Decimal-to-BCD conversion

❑ To express any decimal number in BCD, replace each decimal digit with
the appropriate 4-bit code

❑ Example: Convert the decimal numbers 2469 and 786 to BCD.

❑ Solution:
2469 786
0010010001101001 011110000110
BCD-to-Decimal Conversion

❑ To determine a decimal number from a BCD, start at the right most bit and
break the code into groups of four bits. Then write the decimal digit
represented by each 4-bit group.

❑ Example: Convert the BCD code 1001010001110000 and


0001010010010011 to decimal

❑ Solution:
1001010001110000 0001010010010011

9470 1893
Different Binary codes for the Decimal digits
Digital Codes

❑ Many specialized codes are used in digital systems such as the Gray code
and the ASCII code.

Gray Code:
❑ The Gray code is unweighted (no specific weights assigned to the bit
positions).
❑ It is not an arithmetic code.
❑ It exhibits only a single bit change from one word to the next in sequence.
❑ The Gray code can have any number of bits
Gray Code

❑ The table below lists 4-bit Gray code for decimal numbers 0 through 15.
Binary-to-Gray Code Conversion

❑ To convert a binary number to a Gray code word, follow the rules below:
1. The most significant bit (MSB) in the Gray code is the same as the
corresponding MSB in the binary number.
2. Going from left to right, add each adjacent pair of binary code bits to get
the next Gray code bit. Discard carries.
❑ Example: Convert the binary number 10110 to Gray code
❑ Solution: The Gray code is 11101

1+0+1+1+0

1 1 1 0 1
❑ Example: Convert the binary number 1001010 to Gray code
❑ Solution: Gray = 1101111
Gray-to-Binary Code Conversion

❑ To convert a Gray code word to binary number, follow the rules below:
1. The most significant bit (MSB) in the binary number is the same as the
corresponding MSB in the Gray code.
2. Add each binary code bit generated to the Gray code bit in the next
adjacent position. Discard carries.

❑ Example: convert the Gray code 10101111 to binary


❑ Solution: The binary code is 11001010
1 0 1 0 1 1 1 1

1 1 0 0 1 0 1 0
❑ Example: convert the Gray code 111001 to binary
❑ Solution: The binary code is 101110
Alphanumeric Code
❑ Many applications of digital computers require the handling not only of
numbers, but also of other characters or symbols, such as the letters of the
alphabet.
❑ Alphanumeric codes are codes that represent numbers and alphabetic
characters (letters).
❑ Also most of the alphanumeric codes represent other characters such as
symbols and various instructions necessary for conveying information.
The ASCII is a common alphanumeric code
❑ ASCII is the abbreviation for American Standard Code for Information
Interchange
❑ ASCII is a universally accepted alphanumeric code used in most
computers and other electronic equipment
❑ Most computer keyboards are standardized with ASCII
Alphanumeric code (ASCII)
❑ ASCII has 128 characters and symbols represented by 7-bit binary code.\
❑ ASCII can be considered as 8-bit code with MSB equals 0; this 8-bit code
is 00 through 7F in hexadecimal.
❑ The first thirty-two ASCII characters are nongraphic commands (never
printed or displayed) and are used only for control purposes. An example
of the control characters is “escape.”
❑ The other characters are graphic symbols (can be printed or displayed) and
include the letters of the alphabet (lowercase and uppercase), the ten
decimal digits, punctuation signs, and other commonly used symbols
Alphanumeric code (ASCII)
❑ Example: Determine the binary ASCII codes that are entered from the
computer’s keyboard when the following C ++ language program
statement is typed in.
if (x>5)
❑ Also, express each code in hexadecimal.
❑ Solution:
Alphanumeric code (ASCII)
Alphanumeric code (ASCII)
Alphanumeric code (ASCII)

You might also like