260 likes | 737 Views
Representing Integers in a Computer. In this session we examine the constraints of representing whole numbers by a finite number of bits and the implications for arithmetic. The consequences of having only a finite number of digits.
E N D
Representing Integers in a Computer In this session we examine the constraints of representing whole numbers by a finite number of bits and the implications for arithmetic
The consequences of having only a finite number of digits 9999999 + 1 = 0 ? 9999999 -1 ? 1111111 -1 ?
The possibility of representing negative numbers by ticks away from the top of the clock. • 9999 + 1 = 0000 • 9999 + 2 = 0001 • 9999 + 3218 = (1)0000 – 1 +3218 = 3217 • 9992 + 3218 = (1)0000 – 8 +3218 = 3210 • 7615 + 3218 = (1)0000 – 2385 + 3218 = 833 • If the fifth digit can’t show, it appears correct, but of course there are limitations.
Sixteen-bit integers • Positive 16-bit integers will range from 0000000000000000 to 0111111111111111Negative 16-bit integers from1000000000000000 to 1111111111111111. • That is from 0 to 215-1 and -215 to -1or from -32768 to 32767. • When the correct answer to a computation lies outside this range the answer returned will be wrong. • Negative integers will always have 1 as their first bit; positive integers will have 0.
Finding the negative of a number • Matching pairs that sum to -1, e.g. (7, -8), (6, -7), (5, -6), (4, -5), (3, -4), etc. note that the sum of their binary forms is 1111 (-1) and that the bits are complementary i.e. where one has 1 the other has 0 and vice-versa. To obtain one from the other, reverse the bits --- add 1 to obtain the negative of the number.6 is 0110; reverse the bits to get 1001 (for -7) and add 1 to get 1010 for -6.
The 1’s complement • Reversing the bits or “flipping the switches” in a list of bits is a very easy operation to do in a computer. The result is called the 1’s complement. • In J, if L is a bit list then L=0 gives the 1’s complement. In Matlab L== 0. • To find the negative of an integer, add 1 to it’s 1’s complement (using the full number of bits in the computer representation.
Subtracting binary numbers • Since 2k-1 = 1111 … 1 (k bits) subtracting a k bit binary number, n, from it results in the number obtained from n by flipping over all the binary digits i.e. changing all 1’s to 0’s and all 0’s to 1’s.For example 111111 - 101101 = 010010Writing [1s] for 2k - 1, n and m for k-bit numbers and f(n), f(m) for their 1’s complements, we see thatn – m = ([1s] - f(n)) -m = [1s] - (f(n)+m) = f (f(n) +m)
Complementary numbers on a clock • 41 minutes past the hour is complemented by 19 minutes to the hour; 19 minutes past by 41 minutes to. We tend to use the smaller number. 41 + 19 = 60The complement of 60 is 0.
Adding minutes on a clock face • 35 minutes after 41 past the hour is 76 minutes past the hour, or 16 minutes past the next hour. • 41 + 35 = 76(41- 60) + 35 = 16-19 + 35 = 16 • 35 minutes after 19 to the hour is 16 past.
Minutes to on a clock face are minutes back from 60 or – minutes • 35 minutes after 19 to the hour is 35 + (-19) or16 minutes past. • In a computer, we go the other way and replace -19 by 41
The 2’s Complement • The negative of m is stored as the complement relative to the total number of “minutes in the clock” 27-m . It is called the 2’s complement of m . • The 2’s complement is ((27 -1)- m) +1 or the 1’s complement + 1 • Example: To find the 8-bit representation for -123, note that 123 = 128-5 =8*16-5 = 7*16+11 = 7B16 = 11110112Its 1’s complement is 10000100 (in 8 bits) and so its 2’s complement is 10000101 The 8-bit representation is therefore 10000101. • Check: 1000 0101 = 8516 = 133 = 256 – 123 = 28 -123.
The negative of a k-bit number m is it’s k-bit 2’s complement • Given a positive integer, n, the 2’s complement of n relative to the bit length, k, is the k-bit representation of • 2k nand is the 1's complement incremented by 1. • Example: The negative of -53 in 8 bits is the 2’s complement of 11001011, namely 1+00110100 = 00110101 = 3516 = 53.
1 0 The sign bit • The first bit in a negative integer is always a 1 and for a positive number it is always a 0.This bit is often referred to as the sign bit. • The text treats this bit artificially as somehow separate from the rest of the representation. It is not. For floating point numbers it will be artificially attached, but it a part of the pattern of integer representations.
16-bit Representations Example 1 • Find the 16 bit representation of -30 • 30 in binary (one off 31): 111102 • 16-bit rep.: 0000000000011110 • 1’s complement: 1111111111100001 • 2’s complement: 1111111111100010 • (Sign bit: 1)
16-bit Representations Example 2 Find the 16 bit representations of 339 and -6428 339 = 10*32+19 = 20*16+16+3 = (16+4)16+16+3 = 1(16)2 +5 (16) + 3 = (0001)(0101)(0011) The representation for 339 is 0000000101010011 6428 = 100(64)+28 = 401(16)+12 = (256+145)16+12 = 1(16)3+(9*16+1)16+12 = 191C16 = (0001)(1001)(0001)(1100) = 0001100100011100 The 1's complement is 1110011011100011 The representation for -6428 is 1110011011100100
Arithmetic Overflow • Care must be taken that the arithmetic is within the binary range for the k-bit representation • For an 8-bit representation the range of values is 28-1 n 28-1- 1 i.e. -128 n 127. Any arithmetic outside this range will produce an overflow e.g. 120 + 60120=7*16+8 =01111000 60=3*16+12=00111100120 + 60 = 10110100 = -( 1+f(10110100) ) = - (01001011+1) = - 01001100 = -76 !!! (256 - 180)
Binary Addition (in 8-bit) • Convert to binary: 42 = 1010102 • 26 = 110102 • 8-bit rep of 42: 00101010 • 8-bit rep of 26: 00011010 42 + 26 : 01000100 Find the sum of 42 and 26
Binary Subtraction (in 8-bit) Always done as 42 + (-26) Compare with Slide 7 Find 42 - 26 • 26 = 000110102 42: 00101010 f(26) = 11100101 -26: 11100110 • 100010000 • 000100002 = 16 The additional 1, for which there is no storage space, merely represents the fact that we have gone around the clock.
What’s the big idea? • Remainder arithmetic -- working with remainders. In mathematical jargon these arithmetics are called Z5, Z256 and Z65536 for example • Integers (in k bits) have a limited range • 1’s complement is just “flicking the switches” and 2’s complement just adds 1 to that.