Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representing Integer Data

Similar presentations


Presentation on theme: "Representing Integer Data"— Presentation transcript:

1 Representing Integer Data
ITEC 1000 “Introduction to Information Technology” Lecture 3 Representing Integer Data

2 Lecture Template: Number Representation Unsigned Integer
Complementary Representation 9’s Decimal Representation (skip) 1’s Binary Representation (skip) 10’s Decimal Representation 2’s Binary Representation

3 Number Representation
An integer is a number which has no fractional part. Numbers can be represented as a combination of Sign (plus or minus) Value or magnitude

4 Unsigned Integer (Natural Number)
8-bit storage location 28 different values between 0 and 255 16-bit storage location 216 different values between 0 and 65535 multiple storage locations 4 consecutive 1-byte storage locations provide 32 bits of range 232, or 4,294,967,296 different values difficult to calculate and manipulate

5 32-bit multiple storage location

6 Signed-Integer Representation
No obvious direct way to represent the sign in binary notation Options: Sign-and-magnitude representation 1’s complement (skip – confusing) 2’s complement (most common)

7 Sign-and-Magnitude Use left-most bit for sign
0 = plus; 1 = minus Total range of integers the same Half of integers positive; half negative Magnitude of largest integer half as large Example using 8 bits: Unsigned: = +255 Signed: = = -127 Note: 2 values for 0: +0 ( ) and -0 ( )

8 Calculation Algorithms
Sign-and-magnitude algorithms complex and difficult to implement in hardware Must test for 2 values of 0 Useful with BCD (see slides at end) Order of signed number and carry/borrow makes a difference Example: Decimal addition algorithm Addition: 2 Positive Numbers Addition: 1 Signed Number

9 Ranges No. of bits Binary Unsigned Sign-magnitude Min Max 1 2 3 -1 7
2 3 -1 7 -3 4 15 -7 5 31 -15 6 63 -31 Etc.

10 Ranges: General Rule No. of bits Binary Unsigned Sign-magnitude Min
Max n 2n - 1 -(2n-1 - 1) 2n-1 - 1

11 Complementary Representation
Sign of the number does not have to be handled separately Consistent for all different signed combinations of input numbers Two methods Radix: value used is the base number Diminished radix: value used is the base number minus 1 (skip – confusing) 9’s complement: base 10 diminished radix 1’s complement: base 2 diminished radix

12 10’s Decimal Complement Complement representation: (1) positive number: remains itself; (2) negative number: subtracting its absolute value from a standard basis value Decimal (base 10) system: radix complement Radix = as the basis 3-digit example: base value = 1000 Range of possible values 0 to 999 arbitrarily split at 500 Numbers Negative Positive Representation method Complement Number itself Range of decimal numbers -500 -1 +0 499 Calculation 1000 minus abs(number) none Representation example 500 999 – Increasing value + 1000 – 500

13 10’s Decimal Complement Necessary to specify number of digits or word size 10’s complement representation in “1000” base Example: representation of 3-digit number First digit = 0 through positive number First digit = 5 through negative number

14 Examples with 3-Digit Numbers
10’s complement representation of 247 247 (positive number) Example 2: 10’s complement representation of -247 1000 – 247 = 753 (negative number) Example 3: Find a value with 10’s complement representation 777 Negative number because first digit is 7 1000 – 777 = 223 Signed value = -223

15 10’s Decimal Complement Conversion to sign-and-magnitude value for 9’s complement representation(3 digits) 321 remains +321 521: take the difference: base minus complement (999 – 521) with negative sign: – 479

16 Addition: Counting Upwards
Counting upward on scale corresponds to addition Example in 10’s complement: does not cross the modulus +250 Representation 500 650 900 999 170 420 499 Number represented -500 -350 -100 -1 +0

17 Addition with Wraparound
Count to the right to add a negative number to the representation (since the complement 700 represents the value of -300) Wraparound scale used to extend the range for the negative result +700 Representation 500 999 200 499 900 Number represented -500 -1 -100 -300

18 Addition with End-around Carry
Count to the right crosses the modulus +300 Representation 500 800 999 100 499 Number represented -500 -200 -1 800 300 1100 100

19 Overflow Fixed word size has a fixed range size
Overflow: combination of numbers that adds to result outside the range End-around carry in modular arithmetic avoids problem Complementary arithmetic: numbers out of range have the opposite sign Test: If both inputs to an addition have the same sign and the output sign is different, an overflow occurred

20 2’s Binary Complement Binary system complement
Binary (base 2) system radix complement Radix = as the basis Inversion: change 1’s to 0’s and 0’s to 1s (add 1) Numbers beginning with 0 are positive Numbers beginning with 1 are negative Example with 8-bit binary numbers

21 2’s Complement 2’s complement representation:
Positive value represents itself Negative value: invert and add “1” Numbers Negative Positive Representation method Complement Number itself Range of decimal numbers -12810 -110 +010 12710 Calculation Inversion None Representation example

22 Example: 2’s Complement
Represent –5 in binary using 2’s complement notation Decide on the number of bits: Find the binary representation of the +ve value in 6 bits Invert all the bits: Add 1 6 (for example) 000101 +5 111010 111010 111011 -5

23 Sign Bit in 2’s Complement
In 2’s complement representation, the MSB is the sign bit (as with sign-magnitude notation) 0 = positive value 1 = negative value +5: +ve 5 -5: -ve

24 Estimating Integer Value of 2’s Complement Representation
Positive numbers begin with 0 Small negative numbers (close to 0) begin with multiple 1’s = -2 in 8-bit 2’s complements = -128, largest negative 2’s complements Invert all 1’s and 0’s, add “1” and approximate the value

25 Exercise: 2’s complement conversions
What is -20 expressed as an 8-bit binary number in 2’s complement representation? Answer: is a 7-bit binary number in 2’s complement representation. What is the decimal sign-and-magnitude value? Skip answer Answer

26 Exercise: 2’s complement conversions
Answer What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer: is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer:

27 Detail for > -2010: Positive Value = Invert: Add 1:

28 Detail for > 2’s Complement Rep: Invert: Add One: Converts to: =

29 Arithmetic in 2’s Complement
Add 2 positive 8-bit numbers Add 2 8-bit numbers with different signs Take the 1’s complement of 58 (i.e. invert, add 1) = 45 58 103 = 45 –58 –13 Invert to get magnitude = 13

30 Addition with Carry in 2’s Complement
8-bit number Invert (add 1) (210) Add drop final carry out = 106 –2 104 (drop 1)

31 Subtraction 8-bit number Invert (add 1) 0101 1010 (9010) 1010 0110 Add
drop final carry out = 106 90 (drop 1) 16

32 Overflow 8-bit number Add
256 different numbers Positive numbers: 0 to 127 Add Test for overflow 2 positive inputs produced negative result overflow! Wrong answer! Programmers beware: some high-level languages, e.g., some versions of BASIC, do not check for overflow adequately = 64 65 -127 12710 Invert to get magnitude

33 Overflow and Carry Conditions
Carry flag: set when the result of an addition or subtraction exceeds fixed number of bits allocated Overflow: result of addition or subtraction overflows into the sign bit

34 Overflow/Carry Examples
Correct result No overflow, no carry Example 2: Incorrect result Overflow, no carry 0100 = (+ 4) 0010 + (+ 2) 0110 (+ 6) 0100 = (+ 4) 0110 + (+ 6) 1010 (– 6) 0101 + 1 0110 Invert, then add 1 to get magnitude

35 Overflow/Carry Examples
Result correct ignoring the carry Carry but no overflow Example 4: Incorrect result Overflow, carry ignored 1100 = (– 4) 1110 + (– 2) 11010 (– 6) 1100 = (– 4) 1010 + (– 6) 10110 (+ 3)

36 2’s Complement Subtraction
Just add the opposite value! A – B = A + (-B) add 2’s complement rep. of -B

37 Not quite finished => BCD
Reading: Lecture slides and Chapter 5 Not quite finished => BCD

38 Unsigned Integers Binary equivalent BCD: Binary-Coded Decimal
conversion as discussed in Lecture 2 4 bits can hold 16 different values 0 – 15 8 bits can hold 28 different values between 0 and 255 BCD: Binary-Coded Decimal digit-by-digit individual conversion to binary 4 bits per decimal digit, i.e. 10 different values 0 - 9 8-bit storage location can hold 2 BCD digits, i.e. 100 different values

39 Binary vs. BCD Decimal Binary BCD 68 = 0100 0100 = 0110 1000
= = = = = 68 = = 6 23 = 8 99 (largest 8-bit BCD) = = = = = = 99 = = 255 (largest 8-bit binary) = = = 28 – 1 = 255 8 bits = = 12 bits

40 Value Range: Binary vs. BCD
No. of Bits BCD Range Binary Range 4 0-9 1 digit 0-15 1+ digit 8 0-99 2 digits 0-255 2+ digits 12 0-999 3 digits 0-4,095 3+ digits 16 0-9,999 4 digits 0-65,535 4+ digits 20 0-99,999 5 digits 0-1 million 6 digits 24 0-999,999 0-16 million 7+ digits 32 0-99,999,999 8 digits 0-4 billion 9+ digits 64 0-(1016-1) 16 digits 0-16 quintillion 19+ digits

41 Simple BCD Multiplication

42 Conventional Binary vs. BCD
Binary representation generally preferred greater range of value for given number of bits calculations easier BCD is still used in business applications to maintain decimal rounding and decimal precision in applications with a lot of input and output, but limited calculations


Download ppt "Representing Integer Data"

Similar presentations


Ads by Google