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