Download presentation
Presentation is loading. Please wait.
1
CS 286 Computer Architecture & Organization
Computer Arithmetic Integers and Two’s Complement Department of Computer Science Southern Illinois University Edwardsville Fall, 2019 Dr. Hiroshi Fujinoki Arithmetic/000
2
CS 286 Computer Architecture & Organization
Arithmetic systems used in computers Unsigned Integers Signed Popularly used Integers Fractions Two’s complement Sign Magnitude Not popularly used Arithmetic systems used in computers Popularly used Floating-point numbers Fixed-point numbers Popularly used Not popularly used Computers use binary implementation for these numbers Arithmetic/001
3
CS 286 Computer Architecture & Organization
32(or 64)-bit 2’s complement integer 8-bit unsigned integer IEEE-754 single-precision floating-point 32(or 64)-bit unsigned integer IEEE-754 double-precision floating-point Arithmetic/002
4
CS 286 Computer Architecture & Organization
Unsigned Integers “unsigned” assumed to be “positive” Example: 8-bit unsigned integers (=1) (=2) (=4) (=8) (=16) (=32) (=64) (=128) Weight 20 21 22 23 24 25 26 27 MSB LSB 1 Bit #’s 1 2 3 4 5 6 7 Convert it to a decimal number (27 1) + (26 0) + (25 1) + (24 1) + (23 0) + (22 0) + (21 0) + (20 1) = (27 1) + ((25 1) + (24 1) + (20 1) = = 177 Arithmetic/003
5
CS 286 Computer Architecture & Organization
Properties of unsigned integers The minimum number: 20 21 22 23 24 25 26 27 (=1) (=2) (=4) (=8) (=16) (=32) (=64) (=128) LSB MSB For “0” The maximum number (for 8-bit unsigned numbers): 255 = 28-1 20 21 22 23 24 25 26 27 (=1) (=2) (=4) (=8) (=16) (=32) (=64) (=128) LSB MSB 1 The maximum number (for N-bit unsigned numbers): 2N-1 The value range of N-bit unsigned numbers: between 0 and 2N-1 Arithmetic/004
6
CS 286 Computer Architecture & Organization
Properties of unsigned integers Interval of two numbers: 1 1 1 MAX. MIN. 2N-1 2N-2 1 1 Are all intervals between two consecutive numbers exactly 1? Analysis Given N bits, the number of possible different combinations of 0’s and 1’s will be 2N. Between 0 and 2N-1 (inclusive), there are 2N consecutive distinct integers, for which the interval between the two consecutive integers is always exactly 1. Arithmetic/005
7
CS 286 Computer Architecture & Organization
Example: Addition of two 8-bit unsigned integers as the “bit-wise operation” 20 21 22 23 24 25 26 27 MSB 1 97 + + + + + + + + + MSB 104 1 1 = 11 = 10 = = 1 = = = 1 = 201 MSB 1 1 1 1 = 201 Binary + Operations 1 + + 1 + 1 + 1 + 1 1 10 11 Arithmetic/006
8
CS 286 Computer Architecture & Organization
Example: Addition of two 8-bit unsigned integers as the “bit-wise operation” 27 26 25 24 23 22 21 20 MSB 1 225 + + + + + + + + + 329 104 You need “+256” MSB 1 10 = 11 = 10 = = 1 = = = 1 = Carry Flag 1 MSB 1 1 1 = 73 + 256 Binary + Operations = 329 1 + 11 + 1 + 1 + 1 + 10 Arithmetic/007
9
CS 286 Computer Architecture & Organization
Example: Subtraction of two 8-bit unsigned integers as the “bit-wise operation” 27 26 25 24 23 22 21 20 MSB 1 1 1 104 1 1 1 borrow 1 1 1 - - 7 MSB 97 1 = = = = = 1 = 1 = 1 = = 7 Binary - Operations - 1 - 1 - 1 - Arithmetic/008
10
CS 286 Computer Architecture & Organization
Three problems in unsigned integers Major problems No negative integers Huge numbers require a large # of bits Different operations for additions and subtractions Signed Integers (to solve and ) Sign-magnitude integers Two’s complement integers - Easy to understand for human users Signed integers - Difficult to handle for computer hardware Most of the computer systems use 2’s complement integers - Difficult to understand for human users - Easy to handle for computer hardware Arithmetic/009
11
CS 286 Computer Architecture & Organization
Sign-magnitude integers Use the left-most bit as the sign-bit (0 = positive, 1 = negative) The other bits are treated and used in the exactly the same way as unsigned integers 20 21 22 23 24 25 26 27 MSB 1 104 SIGN 20 21 22 23 24 25 26 MSB 1 +104 MSB 1 -104 Arithmetic/010
12
CS 286 Computer Architecture & Organization
Properties of sign-magnitude integers The maximum number (for 8-bit sign magnitude integers): +127 MSB 1 +127 20 21 22 23 24 25 26 SIGN The minimum number (for 8-bit sign magnitude integers): -127 MSB 20 21 22 23 24 25 26 SIGN 1 -127 For 8-bit sign magnitude integers, Interval of two consecutive numbers: 1 Maximum: Minimum: +2(N-1) - 1 -2(N-1) - 1 Arithmetic/011
13
CS 286 Computer Architecture & Organization
Two problems in sign-magnitude integers Two different expressions for “0” MSB +0 20 21 22 23 24 25 26 SIGN 1 -0 Arithmetic/012
14
CS 286 Computer Architecture & Organization
Two problems in sign-magnitude integers Adding –N to +N does not result in 0 20 21 22 23 24 25 26 SIGN MSB 1 +44 + 1 -44 + -88 1 1 1 1 This does not make sense 1 + 10 Binary + Operations Arithmetic/013
15
CS 286 Computer Architecture & Organization
Two’s complement integers The left-most bit is the sign bit 0 = positive 1 = negative Same as “sign-magnitude integers” For any positive integer, the rest of bits are treated as unsigned (and sign- magnitude integers) Sign-Bit MSB LSB 1 32 +106 64 8 2 “0” is represented by all binary zero’s (e.g., “0000”) The bit pattern of a negative integer can be found by: Obtain the bit pattern of the positive counterpart Invert the bit pattern Add one to it Arithmetic/014
16
CS 286 Computer Architecture & Organization
Two’s complement integers: Bit pattern for negative integers (8 bits) Example - 85 Step 1 Obtain the bit pattern of positive counterpart +85 = 20 21 22 23 24 25 26 SIGN MSB 1 Step 2 Invert the bit pattern MSB 1 1 1 1 Step 3 Add one to it MSB 1 Arithmetic/015
17
Properties in two’s complement integers POSITIVE
POSITIVE -1 7 6 -2 1 5 2 4 -3 3 3 2 1 - 4 4 -1 NEGATIVE -2 5 - 5 -3 -4 - 6 6 -5 -7 7 -6 - 8 -7 -8 Arithmetic/016
18
CS 286 Computer Architecture & Organization
Properties in two’s complement integers 7 The maximum (for 4-bit 2’s complement integers): The minimum (for 4-bit 2’s complement integers): 7 6 -8 5 4 3 The maximum (for N-bit 2’s complement integers): The minimum (for N-bit 2’s complement integers): 2(N-1)-1 2 1 2(N-1) Only one representation for “0” -1 -2 Do we need two procedures for addition and subtraction? -3 -4 -5 -6 -7 -8 Arithmetic/017
19
CS 286 Computer Architecture & Organization
Conversions between two’s complement integers and decimal integers Positive Decimal Binary 2’s complement To indicate “positive” SIGN 26 25 24 23 22 21 20 MSB 1 Example Decimal 85 Decimal 85 = = Arithmetic/018
20
CS 286 Computer Architecture & Organization
Conversions between two’s complement integers and decimal integers Negative Decimal Binary 2’s complement 20 21 22 23 24 25 26 -27 1 2 4 8 16 32 64 -128 MSB 1 ANOTHER TECHNIQUE Example Convert a binary # (- 113) to a 2’s complement integer The decimal # = = (-128) + 15 = (-128) Arithmetic/019
21
CS 286 Computer Architecture & Organization
Conversions between two’s complement integers and decimal integers Positive Binary 2’s complement Decimal - 27 26 25 24 23 22 21 20 MSB 1 Example Convert a binary 2’s complement # to a decimal # ( ) The decimal # = = 85 Arithmetic/020
22
CS 286 Computer Architecture & Organization
Conversions between two’s complement integers and decimal integers Negative Binary 2’s complement Decimal SIGN -128 64 32 16 8 4 2 1 MSB 1 Example Convert a binary 2’s complement # to a decimal # ( ) The decimal # = (-128) = (-128) + 85 = - 43 Arithmetic/021
23
CS 286 Computer Architecture & Organization
Conversions a two’s complement integer to a different bit length 7 6 -1 1 5 -2 2 4 3 -3 3 2 1 - 4 4 -1 -2 5 - 5 -3 -4 - 6 6 -5 -7 7 -6 - 8 -7 Overflow Boundary -8 Arithmetic/022
24
CS 286 Computer Architecture & Organization
Additions and subtractions for two’s complement integers 7 Example #1 Two positive numbers 6 = +3 5 + = +4 4 1 1 1 = +7 3 2 Example #2 Two negative numbers 1 = -4 Still correct! -1 Carry flag + = -1 -2 1 1 1 1 = -5 -3 -4 Example #3 Negative and positive numbers -5 -6 = -7 -7 + = +5 -8 1 1 1 = -2 Arithmetic/023
25
CS 286 Computer Architecture & Organization
Additions and subtractions for two’s complement integers Example #4 Negative and positive numbers 7 6 = -3 Still correct! 5 Carry flag + = +5 4 1 1 = +2 3 2 Example #5 Negative and positive numbers 1 Carry flag = -4 Still correct! + = +4 -1 1 = -0 -2 -3 4 5 -4 -5 -6 -7 -8 Arithmetic/024
26
CS 286 Computer Architecture & Organization
Overflow Flag = 1 Additions and subtractions for two’s complement integers Example #6 Two negative numbers 1 2 3 4 5 6 7 -1 -2 -3 -4 -5 -6 -7 -8 = -7 Incorrect! Carry flag + = -6 1 1 1 = +3 Overflow Flag = 1 Example #7 Two positive numbers = +5 Incorrect! Carry flag + = +4 1 1 = -7 6 7 Arithmetic/025
27
CS 286 Computer Architecture & Organization
Additions and subtractions for two’s complement integers 1 2 3 4 5 6 7 -1 -2 -3 -4 -5 -6 -7 -8 Example #8 Subtract a positive # from a positive # = +3 - = +4 = -4 2‘s Complement = +3 + 1 1 1 1 = -1 For overflow, the same rule applies! Arithmetic/026
28
CS 286 Computer Architecture & Organization
Conversions a two’s complement integer to a different bit length This will work for positive numbers An 8-bit 2’s complement integer 1 MSB LSB MSB 1 LSB A 16-bit 2’s complement integer This will work for negative numbers An 8-bit 2’s complement integer 1 MSB LSB MSB 1 1 LSB A 16-bit 2’s complement integer Arithmetic/027
29
CS 286 Computer Architecture & Organization
Conversions a two’s complement integer to a different bit length This will work for positive numbers 1 MSB LSB An 8-bit 2’s complement integer MSB LSB A 16-bit 2’s complement integer 1 1 MSB LSB An 8-bit 2’s complement integer This won’t work for negative numbers This indicates a negative integer MSB LSB A 16-bit 2’s complement integer 1 Arithmetic/028
30
- 8 4 - 4 2 - 6 -2 6 1 3 5 7 -7 - 5 -3 -1
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.