Basic Definition REPRESENTING INTEGER DATA Englander Ch. 4 An integer is a number which has no fractional part. Examples: -2022-213 0 1 514 323434565232 Unsigned and -Coded Decimal BCD: -Coded Decimal Each decimal digit individually converted to Each decimal digit individually converted to binary Requires 4 bits per digit 8-bit location hold 2 BCD digits 00 to 99 68 10 0110 1000 BCD Hexa: 4 bits can hold 16 values, 0 to F A to F not used in BCD 1
Ranges for Data Formats In General (binary) No. of bits 1 2 3 4 5 6 7 8 9 16 24 Etc. 0 1 0 3 0 7 0 15 0 31 0 63 0 127 0 255 0 511 0-65,535 0 16,777,215 BCD 0 9 0 99 0 9999 0 999999 ASCII 0 9 0 99 0 999 No. of bits Min Max n 0 2 n -1 Remember!! Signed Integers Sign-Magnitude Previous examples were for unsigned integers (positive values only!) Must also have a mechanism to represent signed integers (positive and negative values!) Eg,-5 E.g., 10 =? 2 Two common schemes: 1) sign-magnitude 2) two s complement Extra bit on left to represent sign 0 = positive value 1 = negative value E.g., 6-bit sign-magnitude representation of +5 and 5: +5: 0 0 0 1 0 1-5: 1 0 0 1 0 1 + 5-5 2
Ranges (revisited) Unsigned Sign-magnitude No. of bits Min Max Min Max 1 0 1 2 0 3-1 1 3 0 7-3 3 4 0 15-7 7 5 0 31-15 15 6 0 63-31 31 Etc. In General (revisited) Unsigned Sign-magnitude No. of bits Min Max Min Max n 0 2 n - 1 -(2 n-1-1) 2 n-1-1 Difficulties with Sign-Magnitude Two representations of zero 0: 000000 0: 100000 Arithmetic is awkward! One s complement Principle: Invert bits (0 1 and 1 0) 6: 000110 Range -6: 111001 3
Add / Sub in 1 s complement Overflow Overflow sign of result sign both operands Two s Complement Most common scheme of representing negative numbers in computers Affords natural arithmetic ti (no special rules!) To represent a negative number in 2 s complement notation 1. Decide upon the number of bits (n) 2. Find the binary representation of the positive value in n-bits 3. Flip all the bits (change 1 s to 0 s and vice versa) 4. Add 1 Learn! 4
Two s complement representation Two s Complement Example Represent 5 in binary using 2 s complement notation 1. Decide on the number of bits, for example: 6 2. Find the binary representation of the positive (5) value in 6 bits 000101 +5 3. Flip all the bits 111010 4. Add 1 111010 + 1 111011-5 Sign Bit In 2 s complement notation, the MSB is the sign bit (as with sign-magnitude notation) 0 = positive value 1 = negative value +5: 0 0 0 1 0 1-5: 1 1 1 0 1 1 + 5 -? (previous slide) Complementary Notation Conversions between positive and negative numbers are easy For binary (base 2) 2 s C + - 2 s C 5
Example +5 0 0 0 1 0 1 Detail for -20 11101100 2 s C -5 2 sc 2s 1 1 1 0 1 0 + 1 1 1 1 0 1 1 0 0 0 1 0 0 + 1-20 10 : Positive Vl Value = 00010100 Flip : (One s complement) 11101011 Add 1: + 1 11101100 +5 0 0 0 1 0 1 Detail for 1100011-29 Range for 2 s Complement 2 s Complement: 1100011 Flip : (One s complement) 1011100 Add One: + 1 1011101 Converts to: = - 29 For example, 6-bit 2 s complement notation 100000 100001 111111 000000 000001 011111-32 -31... -1 0 1... 31 Negative, sign bit = 1 Zero or positive, sign bit = 0 kc 6
Ranges (revisited) No. of Unsigned Sign-magnitude 2 s complement bits Min Max Min Max Min Max 1 0 1 2 0 3-1 1-2 1 3 0 7-3 3-4 3 4 0 15-7 7-8 7 5 0 31-15 15-16 15 6 0 63-31 31-32 31 Etc. No. of bits In General (revisited) Unsigned Sign-magnitude 2 s complement Min Max Min Max Min Max n 0 2 n - 1 -(2 n-1-1) 2 n-1-1 -2 n-1 2 n-1-1 To remember 2 s Complement Addition Easy No special rules Just add What is -5 plus +5? Zero, of course, but let s see Sign-magnitude Two s-complement 1 1 1 1 1 1 1 1-5: 10000101-5: 11111011 +5: +00000101 +5: +00000101 10001010 00000000 7
2 s Complement Subtraction Easy No special ilrules Just subtract, well actually just add! What is 10 subtract 3? 7, of course, but Let sdoit(we ll use 6-bit values) 10 3 = 10 + (-3) = 7 A B = A + (-B) add 2 s complement of B +3: 000011 1s C: 111100 +1: 1-3: 111101 001010 +111101 000111 What is 10 subtract -3? 13, of course, but Let s do it (we ll use 6-bit values) (-(-3)) = 3 Overflows and Carries 10 (-3) = 10 + (-(-3)) = 13-3: 111101 1s C: 000010 +1: 1 +3: 000011 001010 +000011 001101 8