3 Lecture Three
3 Lecture Three
3 Lecture Three
By
❑ Signed Numbers.
❑ 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.
❑ 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
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
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?
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
❑ 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:
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
❑ 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.
❑ 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.
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)