Representing Integers in a Computer - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Representing Integers in a Computer

Description:

... of representing negative numbers by ticks away from the top of the clock. ... a clock face ... Minutes to on a clock face are minutes back from 60 or minutes ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 21
Provided by: Win6239
Category:

less

Transcript and Presenter's Notes

Title: Representing Integers in a Computer


1
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

2
The consequences of having only a finite number
of digits
9999999 1 0 ? 9999999 ? -1 ?
1111111 ? -1 ?
3
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 cant show, it appears
    correct, but of course there are limitations.

4
Four-bit integersand constraints on addition
5
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.

6
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.

7
The 1s 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 1s
    complement.
  • In J, if L is a bit list then L0 gives the 1s
    complement. In Matlab L 0.
  • To find the negative of an integer, add 1 to its
    1s complement (using the full number of bits in
    the computer representation.

8
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 1s to 0s and
    all 0s to 1s.For example 111111 - 101101
    010010Writing 1s for 2k - 1, n and m for k-bit
    numbers and f(n), f(m) for their 1s complements,
    we see that n m (1s - f(n)) - m
    1s - (f(n)m) f (f(n) m)

9
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.

10
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.

11
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

12
The 2s 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
    2s complement of m .
  • The 2s complement is ((27 -1)- m) 1 or the
    1s complement 1
  • Example To find the 8-bit representation for
    -123, note that 123 128-5 816-5 71611
    7B16 11110112 Its 1s complement is 10000100
    (in 8 bits) and so its 2s complement is
    10000101 The 8-bit representation is therefore
    10000101.
  • Check 1000 0101 8516 133 256 123 28
    -123.

13
The negative of a k-bit number m is its k-bit
2s complement
  • Given a positive integer, n, the 2s 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 2s
    complement of 11001011, namely 100110100
    00110101 3516 53.

14
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.

15
16-bit Representations Example 1
  • Find the 16 bit representation of -30
  • 30 in binary (one off 31) 111102
  • 16-bit rep. 0000000000011110
  • 1s complement 1111111111100001
  • 2s complement 1111111111100010
  • (Sign bit 1)

16
16-bit Representations Example 2
Find the 16 bit representations of 339 and -6428
339 103219 2016163 (164)16163
1(16)2 5 (16) 3 (0001)(0101)(0011)
The representation for 339 is 0000000101010011
6428 100(64)28 401(16)12
(256145)1612 1(16)3(9161)1612
191C16 (0001)(1001)(0001)(1100)
0001100100011100
The 1's complement is
1110011011100011
The representation for -6428 is 1110011011100100
17
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 601207168
    01111000 603161200111100120 60
    10110100 -( 1f(10110100) ) - (010010111)
    - 01001100 -76 !!! (256 - 180)

18
Binary Addition (in 8-bit)
Find the sum of 42 and 26
  • Convert to binary 42 1010102
  • 26 110102
  • 8-bit rep of 42 00101010
  • 8-bit rep of 26 00011010 42 26
    01000100

19
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.
20
Whats 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
  • 1s complement is just flicking the switches
    and 2s complement just adds 1 to that.
Write a Comment
User Comments (0)
About PowerShow.com