You are on page 1of 14

Chapter 4 – Representing Integer Data

·         This chapter covers the following main topics:


o        Unsigned integer representation
o        Signed integer representation
o        Overflow and carry conditions

Introduction

·         Sign-and-magnitude representation is the conventional way for representing signed integer


numbers
·         The number is represented as a sign followed by a magnitude (e.g. +50, -40)
·         A number that contains no sign is considered positive (i.e. positive sign is implied)
·         Computer architecture provides no special provision for representing sign integer in the same way
as it is represented in the conventional way
·         One possible solution is to store the number as a string of characters
·         However, calculations using this methods is not practical nor efficient
·         In this chapter we will learn how computers represent signed integer using sign-
free representation
·         These representation methods are both practical and efficient

Unsigned Integer Representation

·         Unsigned integer representation is simple as there is no need to represent the sign


·         There exist 2 methods for representing unsigned integer in computers
o        Direct Binary Equivalent
o        Binary-coded Decimal (BCD)
 
Direct Binary Equivalent
·         This method provides direct binary equivalent for the decimal number
·         A decimal number is represented in binary by performing normal decimal-to-binary conversion
using similar methods as learned in chapter 2
·         The range of the number that can be represented using this method depends on storage size
R = 2n             (R = Range, n = number of storage bits)
·         The range starts at 0 and ends at 2n - 1
Range for:
8-bit   = 0 - 28 - 1    = 0-255
16-bit = 0 – 216 - 1  = 0-65535
32-bit = 0 – 232  - 1 = 0-4294967296
 
Examples
1. Represent 14110 in 16-bit direct binary equivalent representation
o        Use the division by base method (learned in chapter 2) to convert the decimal number to
binary
= 100011012
o        Pad with zeros to represent all storage bits
= 00000000100011012
 
2. Convert 100011012 represented in direct binary equivalent to decimal
o        Use the sum of digits weight method (learned in chapter 2) to convert the binary number to
decimal
= 1 + 4 + 8 + 128
= 14110
 
Binary-Coded Decimal (BCD)
·         A number is represented as digit-by-digit binary representation of the decimal integer
·         Each decimal digit is directly converted to its equivalent binary digits as if they are related
·         Each decimal digit requires 4 binary digits
·         Use a conversion table, as seen in chapter 2, to perform the conversion
·         Each 4 binary digits is converted to a single decimal digit when performing binary to decimal
conversion
 
Advantage
·         Translation between BCD and alphanumeric is very easy
·         Subtract 3016 to convert from character digit to numerical digit
·         Add 3016 to convert from numerical digit to character digit
o        Numbers are inputted into the computer as alphanumeric
o        Conversion between alphanumeric and numeric (back and forth) is always necessary
o        Very efficient when the application has a lot of input and output and limited calculations
 
Disadvantage
·         Waste of storage since range of values is substantially lower (e.g. 100M vs 4G for 32-bit)
o        8-bit = 0-99, 16-bit = 0-9999, 32-bit = 0-99999999
·         Calculations is not efficient, see example 4.3 in page 99
o        Number must be grouped by 4-bit to correspond to each decimal digit
o        Calculation of each 4-bit group must be treated individually
o        Product or sum that exceeds 9 must be converted to BCD to perform the carry from one
digit to another
 
Examples
1. Represent 14110 in 16-bit BCD representation
o        Use the digit by digit direct conversion method (learned in chapter 2) to convert the decimal
number to binary
= 0000 0001 0100 00012
 
2. Convert 00000001010000012 represented in BCD to decimal
o        Group by 4 and perform direct binary to decimal conversion of each group
= 0000 0001 0100 0001
= 014110

Signed Integer Representation

·         Signed integer representation, is more complicated than unsigned integer representation


·         The sign of the number must be represented
·         There are 2 different methods used to represent signed integer in computers
·         Sign-and-magnitude representation
·         Complementary representation
 
Sign-and-magnitude Representation
·         The sign is represented followed by the number magnitude
·         A sign bit, typically the leftmost bit, (0 for +, 1 for -) is used to represent the sign
·         The positive range is one-half of the range and the negative range is makes up the second half
Example: 16-bit (1 bit sign and 15 bit magnitude) have a positive range of: +0 to +32767 and
negative range of and -0 to –32767
·         Note that the total range is the same but redistributed between the positive side and the negative
side
 
Disadvantage
·         There are 2 different values for the zero (i.e. +0 and –0)
o        The system must test at the end of every calculation to ensure the presence of a single 0
·         Calculations is very complex and difficult to implement in hardware (i.e. no consistent way of
performing the calculations)
Example 1:
4+2 = 6
4-2 = 2
2-4 = -2
Example 2: The presence of a second digit that result in a carry or borrow may change the result
12-4 = 8
 
Complementary Representation
·         Complementary representation is the prevalent method for representing signed integer in
computers
·         Every number system has 2 forms of complementary representations
o        Base-1’s complement
o        Base’s complement
 
·         2 forms of binary complementary representation
o        1’s complement
o        2’s complement
·         2 forms of decimal complementary representation
o        9’s complement
o        10’s complement
·         2 forms of hex complementary representation
o        15’s complement
o        16’s complement
·         2 forms of octal complementary representation
o        7’s complement
o        8’s complement
 
Advantage
·         Provide sign free representation
·         The sign of the number does not need special handling
·         Calculations is simple and consistent for all different signed combination of numbers
 
·         2 forms of binary complementary representation are in common use in computers
o        1’s complement
o        2’s complement
·         For simplicity lets first start by studying the decimal complementary representation:
o        9’s complement
o        10’s complement

9’s Complement Representation

·         9’s complement is the decimal counter part of the binary 1’s complement
·         Figure 4.5 in page 103 illustrates 3-digit number representation in 9’s complement
·         Positive range matches the sign-and-magnitude representation positive range
·         Negative range is different than the sign-and-magnitude representation negative range
·         The sign of a number is determined by checking the most significant digit
o        Positive          when most significant digit values 0-4
o        Negative        when most significant digit values 5-9
 
Convert from sign-and-magnitude to 9’s complement
·         Must specify the number of 9’s complement storage digits available to represent the number
·         If number is positive no conversion is necessary (i.e. same as sign-and-magnitude representation)
o        Pad the number with 0’s, if needed, to represent all the assigned number of storage digits
·         If number is negative conversion is required (complement the number)
o        Complementary base – number
·         The complementary base is the highest value in the number supported by the given number of
digits
·         Complementary base for:
o        3 digits = 999
o        5 digits = 99999
 
Examples
1. Represent –467 in 3-digit 9’s complement
o        The number is negative so complement the number = 999 – 467
                = 532
 
2. Represent –467 in 4-digit 9’s complement
o        The number is negative so complement the number = 9999 – 467
                = 9532
 
3. Represent 467 in 3-digit 9’s complement
o        The number is positive so the 9’s complement is the same as the number
                = 467
 
4. Represent 667 in 4-digit 9’s complement
o        The number is positive so the 9’s complement is the same as the number
o        Need to pad with zero to fill the entire storage space
                = 0667
 
5. Represent 667 in 3-digit 9’s complement
o        The number cannot be represented with only 3-digits (overflow occur)
o        Maximum positive number the can be represent is 499
 
Convert from 9’s complement to sign-and-magnitude
·         Determine whether the number is positive or negative (inspect the most significant digit)
·         If the number is positive, the sign-and-magnitude equivalent is the same as the 9’s complement
·         If the number is negative, complement the number
o        Complementary base – number
o        Add the negative sign
 
Examples
1. Convert 9990 to sign-and-magnitude representation
o The number is negative (most significant digit is 9)
o Complement the number:    9999 – 9990 = 9
o Add negative sign
= -9
 
2. Convert 595 to sign-and-magnitude representation
o The number is negative (most significant digit is 5)
o Complement the number:    999 – 595 = 404
o Add negative sign
= -404
 
3. Convert 4990 to sign-and-magnitude representation
o The number is positive (most significant digit is 4)
o The result is the same as the 9’s complement
= 4990
 
Addition and Subtraction Arithmetic
·         Addition in 9’s complement is done using the following method:
o        Add the 2 numbers in the normal way
o        Add 1 to the result if there is end-round carry
·         An end-round carry occur if the result overflow the storage space
 
·         Subtraction in 9’s complement is done using the following method:
o        Complement any number with negative sign (i.e. eliminate the negative sign)
o        Add the two numbers using the same method as the “Addition in 9’s Complement”
 
Examples
·         Add (799 + 100)
o 799 + 100
= 899
 
·         Add (799 + 300)
o 799 + 300 =(1)099
o Add end-round carry = 1 + 099
= 100
 
·         Add (0799 + 0300)
o 0799 + 0300
= 1099
 
·         Subtract (799 – 100)
o Find 9’s complement of 100 = 999 – 100 = 899
o Add 799 + 899 = (1) 698
o Add the end-round carry = 1 + 698
= 699
 
·         Subtract (106 – 090)
o Find 9’s complement of 90 = 999 – 90 = 909
o Add 106 + 909 = (1)015
o Add end-round carry = 1 + 015
= 016
 
·         Subtract (0106 – 0090)
o Find 9’s complement of 90 = 9999 – 90 = 9909
o Add 0106 + 9909 = (1) 0015
o Add end-round carry = 1 + 0015
= 0016
 
·         Subtract (-40 - 2)
o Find 9’s complement of 40 = 99 – 40 = 59
o Find 9’s complement of 2 = 99 – 2 = 97
o Add 59 + 97 = (1) 56
o Add end-round carry = 1 + 56
= 57

10’s Complement Representation


·         The 10’s complement solves the dual zero problem in 9’s complement
·         See Figure 4.11 in page 110 for 10’s complement representation of 3-digit decimal number
·         10’s complement = 9’s complement + 1
 
Convert from sign-and-magnitude to 10’s complement
·         Similar to 9’s complement with the following differences
o        Complementary base is 1 more than the 9’s complement (i.e. 9’s complement + 1)
 
Examples
·         Represent –467 in 3-digit 10’s complement
o        The number is negative so complement the number = 1000 – 467
= 533
 
·         Represent –467 in 4-digit 10’s complement
o        The number is negative so complement the number = 10000 – 467
= 9533
 
·         Represent 467 in 3-digit 10’s complement
o        The number is positive so the 10’s complement is the same as the number
= 467
 
·         Represent 667 in 4-digit 10’s complement
o        The number is positive so the 10’s complement is the same as the number
= 0667
 
·         Represent 667 in 3-digit 10’s complement
o        The number cannot be represented with only 3-digits (overflow)
 
Convert 10’s complement to sign-and-magnitude
·         Similar to 9’s complement with the following differences
o        Complementary base is 1 more than the 9’s complement (i.e. 9’s complement + 1)
 
Examples
·         Convert 9990 to sign-and-magnitude representation
o The number is negative (most significant digit is 9)
o Find 10’s complement = 10000 – 9990 = 10
o Add negative sign
= -10
 
·         Convert 595 to sign-and-magnitude representation
o The number is negative (most significant digit is 5)
o Complement the number:    1000 – 595 = 405
o Add negative sign
= -405
 
2. Convert 4990 to sign-and-magnitude representation
o The number is positive (most significant digit is 4)
o The result is the same as the 10’s complement
= 4990
 
Addition and Subtraction Arithmetic
·         Addition in 10’s complement is done using the following method:
o        Add the two numbers 
o        Carry beyond the specified number of digits is dropped
 
·         Subtraction in 10’s complement is done using the following method:
o        Find the 10’s complement of any number with negative sign (i.e. eliminate the negative
sign)
o        Add using the same method as the “Addition in 10’s Complement”
 
Examples
1. Add (799 + 100)
o 799 + 100
= 899
 
2. Add (799 + 300)
o 799 + 300 = (1)099
o Drop the end-round carry
= 099
 
3. Add (0799 + 0300)
o 0799 + 0300
= 1099
 
4. Subtract (799 – 100)
o Find the 10’s complement of 100 = 1000 – 100 = 900
o Add 799 + 900 = (1) 699
o Drop the end-round carry
= 699
 
5. Subtract (106 – 090)
o Find the 10’s complement of 90 = 1000 – 90 = 910
o Add 106 + 910 = (1) 016
o Drop the end-round carry
= 016
 
6. Subtract (0106 – 0090)
o Find the 10’s complement of 0090 = 10000 – 90 = 9910
o Add 0106 + 9910 = (1) 0016
o Drop the end-round carry
= 0016

1’s Complement Representation

·         1’s complement is a complementary representation method for representing signed integer in


binary
·         1’s complement is the counter part of 9’s complement in decimal
·         See Figure 4.10 in page 108 for illustration of 8-bit 1’s complement representation
·         The sign of a number is determined by checking the most significant bit
o        Positive          when most significant bit is 0
o        Negative        when most significant bit is 1
·         1’s complement is performed by inverting every bit (0 becomes 1 and 1 becomes 0)
 
Find 1’s complement
·         Pad the number with 0’s if needed to represent all the assigned number of storage bits
·         Invert the bits
 
Examples
·         Find the 8-bit 1’s of the binary number 00101101
o        Invert to find the 1’s complement
= 11010010
 
·         Find the 16-bit 1’s of the binary number 00101101
o        Pad with 0’s to get 16-bit representation = 0000000000101101
o        Invert to find the 1’s complement
= 1111111111010010
 
Convert from decimal sign-and-magnitude to 1’s complement
·         Convert the decimal number to binary
·         Pad with zeros to fill the entire storage space
·         Determine whether the number is positive or negative
·         If the number is positive, no need to complement the number
·         If the number is negative, you need to complement the number
 
Examples
·         Convert –4510 to 8-bit 1’s complement
o Convert from decimal to binary = 101101
o Pad with 0’s to get 8-bit representation = 00101101
o The number is negative (the sign is -) so invert the bits
= 11010010
 
·         Convert 4510 to 16-bit 1’s complement
o Convert from decimal to binary = 101101
o Pad with 0’s to get 16-bit representation = 0000000000101101
o The number is positive so no need to complement
= 0000000000101101
 
3. Convert -2510 to 16-bit 1’s complement
o Convert from decimal to binary = 11001
o Pad with 0’s to get 16-bit representation = 0000000000011001
o The number is negative so invert the bits
= 1111111111100110
 
Convert from 1’s complement to decimal sign-and-magnitude
·         Determine whether the number is positive or negative
·         If the number is positive, no need to complement the number
·         If the number is negative, you need to complement the number
·         Convert to decimal
·         Add the negative sign if the number is negative
 
Examples
·         Convert 00101101 to decimal sign-and-magnitude
o        The number is positive (most significant bit is 0), no need to complement
o        Convert to decimal = 45
= 45
 
·         Convert 11010010 to decimal sign-and-magnitude
o        The number is negative (most significant bit is 1), then first find the complement of the
number
o Find 1’s complement = 00101101
o Convert to decimal = 45
o        Add the negative sign
= -45
 
·         Convert 1111111111100110 to decimal sign-and-magnitude
o        The number is negative (most significant bit is 1), then first find the complement of the
number
o Find 1’s complement = 0000000000011001
o Convert to decimal = 25
o        Add the negative sign
= -25
 
Addition and Subtraction Arithmetic
·         Addition in 1’s complement is done using the following method
o        Add the two numbers
o        If there is an end-round carry, add the carry to the result
 
·         Subtraction in 1’s complement is done using the following method
o        Find 1’s complement of the number with negative sign in front (i.e. eliminate negative sign)
o        Adding the two numbers
 
Examples
1. Add (00101101 + 00111010)                            (i.e. 45 + 58 = 103)
o        00101101 + 00111010
= 01100111                                                     = 103 in decimal
 
2. Add (01101010 + 11111101)                            (i.e. 106 - 2 = 104)
o        01101010 + 11111101 = (1) 01100111                 
o        Add end-carry to the result = 1 + 01100111
= 01101000                                                     = 104 in decimal
 
3. Subtract (00101101 - 00111010)                      (i.e. 45 - 58 = -13)
o Find 1’s complement of the second number = 11000101
o Add the 2 numbers = 00101101 + 11000101 = 11110010
= 11110010                                                     = -13 in decimal
 
4. Subtract (01101010 – 11111101)     (i.e. 106 - -2 = 106 + 2 = 108)
o Find 1’s complement of the second number = 00000010
o Add the 2 numbers = 01101010 + 00000010 = 01101100
= 01101100                                                     = 108 in decimal
 
2’s Complement Representation
·         The 2’s complement is an alternative complementary representation for signed integers
·         See Figure 4.1 for 2’s complement representation of 8-bit number
·         2’s complement = 1’s complement + 1
·         2’s complement solves the dual zero problem (i.e. –0 and +0) found in 1’s complement
·         The –0 is eliminated and the negative scale is shifted to the right by 1 (i.e. add 1)
·         2’s complement is more common than 1’s complement in computers
 
Advantages
·         Addition arithmetic is faster as it does not require an extra end-round carry step
·         2’s complement solves the dual zero
 
Disadvantages
·         Finding complement requires one extra step (i.e. adding 1 after inversion)
 
Convert from decimal sign-and-magnitude to 2’s complement
·         Similar to 1’s complement with the following differences:
o        Complement number = invert bits + 1
 
Examples
·         Convert –4510 to 8-bit 2’s complement
o The number is negative (the sign is -)
o Convert from decimal to binary = 101101
o Pad with 0’s to get 8-bit representation = 00101101
o Invert the bits = 11010010
o Add 1
= 11010011
 
·         Convert 4510 to 16-bit 2’s complement
o The number is positive
o Convert from decimal to binary = 101101
o Pad with 0’s to get 16-bit representation = 0000000000101101
= 0000000000101101
 
·         Convert -2510 to 16-bit 1’s complement
o The number is negative (the sign is -)
o Convert from decimal to binary = 11001
o Pad with 0’s to get 16-bit representation = 0000000000011001
o Invert the bits = 1111111111100110
o Add 1
= 1111111111100111
 
Convert from 2’s complement to decimal sign-and-magnitude
·         Similar to 1’s complement with the following differences:
o        Complement number = invert bits + 1
 
Examples
·         Convert 00101101 to decimal sign-and-magnitude
o        The number is positive (most significant bit is 0), no need to complement
o        Convert to decimal = 45
= 45
 
·         Convert 11010010 to decimal sign-and-magnitude
o        The number is negative (most significant bit is 1), then first find the complement of the
number
o Find 2’s complement = 00101101 + 1 = 00101110
o Convert to decimal = 46
o        Since the number is negative, add the negative sign
= -46
 
·         Convert 1111111111100110 to decimal sign-and-magnitude
o        The number is negative (most significant bit is 1), then first find the complement of the
number
o Find 2’s complement = 0000000000011001 + 1 = 0000000000011010
o Convert to decimal = 26
o        Since the number is negative, add the negative sign
= -26
 
Addition and Subtraction Arithmetic
·         Addition in 2’s complement is similar to 1’s complement with the following differences:
o        The end-carry is dropped rather than added to the result
 
·         Subtraction in 2’s complement is similar to 1’s complement with the exception
o        2’s complement is found for numbers with negative sign instead of 1’s complement
 
Examples
1. Add (01101010 + 11111101)                            (i.e. 106 - 3 = 103)
o 01101010 + 11111101 = (1) 01100111
o Drop the end carry
= 01100111                                                     = 103 in decimal
 
2. Add (00101101 + 00111010)                            (i.e. 45 + 58 = 103)
o 00101101 + 00111010
= 01100111                                                     = 103 in decimal
 
 
3. Subtract (01101010 - 11111101)                      (i.e. 106 - -3 = 106 + 3 = 109)
o Complement the second number to eliminate the negative sign = 00000010 + 1 = 00000011
o 01101010 + 00000011
= 01101101                     = 109 in decimal
 
4. Subtract  (00101101 - 00111010)     (i.e. 45 - 58 = -13)
o Complement the second number to eliminate the negative sign = 11000101 + 1 = 11000110
o 00101101 + 11000110
= 11110011                                                     = -13 in decimal
Overflow and Carry Conditions

·         Typically, an overflow occur when the result of calculation does not fit into the storage space
·         In 2’s complement an addition or subtraction overflow occur when the result overflows into the
sign bit
·         An overflow can be detected when the sign of the result is an opposite of the sign of both numbers
·         In computers, an overflow flag is used to test for an overflow (flag is set/reset at every
calculation)
 
·         In 2’s complement an addition or subtraction carry occur when the result exceeds the storage
space
·         A carry flag is used to test for carry (flag is set/reset at every calculation)
·         Overflow and carry can occur independent of each other
·         4 possible outcomes as a result of addition or subtraction
o        No overflow and No carry
o        No overflow and carry
o        Overflow and no carry
o        Overflow and carry
·         Overflow produces an incorrect result
·         Carry still produces a correct result
 
Examples (addition of two 4-bit 2’s complement numbers)
o        0100 + 0010 = 0110                       (4+2 = 6) no overflow and no carry (result is correct)
o        0100 + 0110 = 1010                       (4+6 = -6)               overflow and no carry (result is
incorrect)
o        1100 + 1110 = (1) 1010  (-4-2 = -6)               no overflow and carry (result is correct)
o        1100 + 1010 = (1) 0110  (-4-6 = 6)                overflow and carry (result is incorrect)

You might also like