Discrete Maths 2003 Lecture 05 3 Slides Pp

Embed Size (px)

Citation preview

  • 8/13/2019 Discrete Maths 2003 Lecture 05 3 Slides Pp

    1/4

    Lecture 5, 31-July-20Discrete Mathematics 2003

    1

    Storing Integers in a Computer

    For simplicity, were looking at how integers

    are stored in 2 bytes (16 bits), though typicallya processor has 4 bytes available

    Recall (previous lecture) that 216 = 65,536integers can be stored in 16 bits

    Its usually better to have about the same no. ofpositive & negative integers available

    Hence the range 32,768 n 32,767 ispreferred (this range is 215 n 215 1)

    2

    Storing an Integer n in 16 Bits

    The first bit is the sign bit, which is 0 if n ispositive or zero, & 1 if n is negative

    If n 0, the remaining 15 bits are the binary repnof n (with leading zeros if necessary)

    If n < 0, the remaining 15 bits are the binary repnof n + 32,768

    If 32767 n 1, its easier to use the (15-bit)2s complementto find the computer repn

    To find the 2s complement, write the pos. no. asa 15-bit binary, retain all 0s at the right end & therightmost 1, and reverse all other bits

    3

    Examples of Computer

    Representations

    Examples: Find the 16-bit computerrepresentations of the following integers:

    (a): 12723 (i.e. 110001101100112) (b): 3080 (i.e. 1100000010002)

    (c): 32768 (i.e. 10000000000000002)

    Exercise: Find the 16-bit computer repn of5131 (i.e. 10100000010112)

    Answer: 1110101111110101

  • 8/13/2019 Discrete Maths 2003 Lecture 05 3 Slides Pp

    2/4

    Lecture 5, 31-July-20Discrete Mathematics 2003

    4

    Storing an Integer n in 32 Bits

    We know how a computer with 2 bytes (16

    bits) for integers can store nos in the range of32,768 n 32,767, or 215 n 215 1

    For a computer that uses 4 bytes (32 bits) forintegers, the range is 231 n 231 1, whichis 2,147,483,648 n 2,147,483,647

    n is stored in 32 bits in the same way as for 16bits, except 2,147,483,648 replaces 32,768, &binary conversions are to 31 bits, not 15

    5

    3.3 Arithmetic with Integers

    Well now look at how computers actuallyadd & subtract integers

    However, to keep nos to a manageable size,well assume they are stored usingfourbits,rather than a more realistic 16 or 32 bits

    In 4 bits, we can represent 24 = 16 integers,in the range 23 n 23 1 (i.e. 8 n 7)

    4-bit representations are obtained in thesame way as for 16 bits (but the calculations

    are simpler)

    6

    4-bit Representations of Integers

    The first bit is the sign bit, which is 0 if n ispositive or zero, & 1 if n is negative

    If n 0, the remaining 3 bits are the binary repnof n (with leading zeros if necessary)

    If n < 0, the remaining 3 bits are the binary repnof n + 8

    Alternately, if 7 n 1, the remaining 3 bitscan be found by using the 3-bit 2s complement

    Example: The 4-bit representations of 6 and 5are 0110 and 1011, respectively

  • 8/13/2019 Discrete Maths 2003 Lecture 05 3 Slides Pp

    3/4

    Lecture 5, 31-July-20Discrete Mathematics 2003

    7

    Table of 4-bit Representations

    Inte er Re n Inte er Re n

    8 1000 0 00007 1001 1 0001

    6 1010 2 0010

    5 1011 3 0011

    4 1100 4 0100

    3 1101 5 0101

    2 1110 6 0110

    1 1111 7 0111

    8

    4-bit Integer Representations

    Notice from the table that:

    For nos between 0 & 7, the computer repn is

    thesame as the (4-bit) binary repn

    For nos between 8 & 1, the computer repn

    of n is 1 (the sign bit), followed by the 3-bit

    binary repn of n + 8. Therefore the computer

    repn of n is the binary repn of n +16.

    e.g. the computer repn of 3 is 1101, which

    is the binary repn of 13

    9

    Adding Integers on a 4-bit Computer

    Now look at adding 2 nos on a 4-bit

    computer

    Sometimes this cant be done e.g. theresult of 4 + 6 is too large to be represented

    However, provided we look only at nos that

    canbe added, addition is carried out as

    usual for binary nos, exceptthat a 1 in the

    5th column from the right is ignored

  • 8/13/2019 Discrete Maths 2003 Lecture 05 3 Slides Pp

    4/4

    Lecture 5, 31-July-20Discrete Mathematics 2003

    10

    Examples of Addition on a 4-bit

    Computer

    Example: Verify that the addition (2) + 6

    is carried out correctly on a 4-bit computer

    Exercise: Verify that a 4-bit computer

    correctly carries out the addition (1) + (5)