CS61C Numbers Galore Lecture 2 - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

CS61C Numbers Galore Lecture 2

Description:

Become familiar with how to do basic arithmetic with them ... is a hassle both to programmers and to hardware designers (since technically 0 == -0) ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 34
Provided by: davep165
Category:

less

Transcript and Presenter's Notes

Title: CS61C Numbers Galore Lecture 2


1
CS61CNumbers Galore Lecture 2
  • June 22, 1999
  • Nemanja Isailovic

2
Overview
  • Base Systems
  • Negative Numbers
  • Overflow

3
General Numbers Base B
  • Base B ? B symbols per digit
  • Base 10 (Decimal) 0,1,2,3,4,5,6,7,8,9 Base 2
    (Binary) 0,1
  • Number representation
  • d31d30d29...d2d1d0 32 digit number
  • ? d31 x B31 d30 x B30 ... d2 x B2
    d1 x B1 d0 x B0

4
Decimal Numbers Base 10
  • Digits 0,1,2,3,4,5,6,7,8,9
  • Example
  • 8439503 (8x106) (4x105) (3x104) (9x103)
    (5x102) (0x101) (3x100)

5
Binary Numbers Base 2
  • Digits 0,1
  • Example (convert binary to decimal)
  • 1011010 (1x26) (0x25) (1x24) (1x23)
    (0x22) (1x21) (0x20)
  • 64 0 16 8 0 2 0
  • 90 decimal
  • Notice that a 7 digit binary number turns out to
    be a 2 digit decimal number

6
Hexadecimal Numbers Base 16 (1/2)
  • Digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  • Normal digits have expected values
  • In addition
  • A ? 10
  • B ? 11
  • C ? 12
  • D ? 13
  • E ? 14
  • F ? 15

7
Hexadecimal Numbers Base 16 (2/2)
  • Example (convert hex to decimal)
  • B28F0DD (Bx166) (2x165) (8x164) (Fx163)
    (0x162) (Dx161) (Dx160)
  • (11x166) (2x165) (8x164) (15x163)
    (0x162) (13x161) (13x160)
  • 187232477 decimal
  • Notice that a 7 digit hex number turns out to be
    a 9 digit decimal number

8
Hex to Binary Conversion
  • Each hex digit represents 16 decimal values.
  • Four binary digits represent 16 decimal values.
  • Therefore, each hex digit can replace four binary
    digits.
  • Examples
  • 1010 1100 0101 (binary) AC5 (hex)
  • 10111 (bin) 0001 0111 (bin) 17 (hex)
  • 3F9 (hex) 0011 1111 1001 (binary)

9
How Do We Tell the Difference?
  • In general, append a subscript at the end of a
    number stating the base
  • 1010 is in decimal
  • 102 is binary ( 210)
  • 1016 is hex ( 1610)
  • When dealing with computers
  • Hex numbers are preceded with 0x
  • 0x10 1016 1610
  • Binary and Decimal numbers should be clearly
    specified

10
Which Base Should We Use?
  • Decimal Great for humans most arithmetic is
    done with these.
  • Binary This is what computers use, so get used
    to them. Become familiar with how to do basic
    arithmetic with them (,-,,/).
  • Hex Terrible for arithmetic but if we are
    looking at long strings of binary numbers, its
    much easier to convert them to hex in order to
    look at four bits at a time. (Never do
    arithmetic in hex, though.)

11
Inside the Computer
  • To a computer, numbers are always in binary all
    that matters is how they are printed out binary,
    decimal, hex, etc.
  • As a result, it doesnt matter what base a number
    in C is in...
  • 3210 0x20 1000002
  • only the value of the number matters.
    (Remember this for your projects.)

12
What about Negative Numbers?
  • Up until now, weve only determined how to write
    positive numbers.
  • In math, we usually write numbers as a numerical
    value preceded by a sign
  • 14
  • -413
  • So, it would be logical to extend this to
    computer representation of numbers...

13
Sign-Magnitude
  • A MIPS computer uses 32-bit integers.
  • Define leftmost bit (Little Endian bit 31) to be
    the sign bit
  • 1 for negative, 0 for positive
  • Let the other 31 bits be the numerical value of
    the number.
  • Examples...

14
Drawbacks of Sign-Magnitude
  • Primary Difficult to do arithmetic
  • When adding, must perform special steps depending
    on whether the signs are the same or different
  • Examples
  • Secondary Two zeros
  • 0x00000000 0 decimal
  • 0x80000000 -0 decimal
  • This is a hassle both to programmers and to
    hardware designers (since technically 0 -0).

15
Ones Complement
  • Sign-Magnitude was abandoned.
  • New Idea Again, all positive numbers start with
    a 0, but now define negative number to be inverse
    of positive.
  • 0x00000001 1 decimal so 0xFFFFFFFE -1
    decimal
  • 0x0000000F 15 decimal
    so 0xFFFFFFF0 -15 decimal
  • Notice that positive numbers have leading 0s and
    negative numbers have leading 1s.

16
Analysis of Ones Complement
  • Arithmetic is not too hard.
  • Primary Drawback
  • 0x00000000 0 decimal
  • 0xFFFFFFFF -0 decimal
  • There are still two zeros, which makes arithmetic
    and hardware much more complicated.

17
Administrivia
  • Remember to show up to lab so TA can get your
    name Telebears doesnt guarantee anything.
  • Fill out online survey.
  • Homework 1 is on the web.
  • Get started on it now!

18
Twos Complement (1/2)
  • Extension on New Idea All positive numbers still
    start with a 0, but now define negative number to
    be inverse of positive PLUS ONE.
  • 0x00000001 1 decimal so -1 decimal
    0xFFFFFFFE 0x1 0xFFFFFFFF
  • 0x0000000F 15 decimal
    so -15 decimal 0xFFFFFFF0 0x1
    0xFFFFFFF1
  • Notice that positive numbers still have leading
    0s and negative numbers still have leading 1s.

19
Twos Complement (2/2)
  • Remember To calculate negative number, write
    down positive number, invert the bits and add
    one.
  • Example
  • 5 000000101
  • invert the bits 111111010
  • add one 111111011 -5
  • Weird but True Just like positive numbers can
    have infinitely many leading 0s, negative numbers
    can have infinitely many leading 1s.

20
Analysis of Twos Complement (1/2)
  • Arithmetic works
  • 7 0000111
  • -5 1111011
  • 2 0000010
  • No special cases just add!
  • Only one zero
  • 0 00000000
  • invert 11111111
  • add one 00000000 -0

21
Analysis of Twos Complement (2/2)
  • 0000 ... 0000 0000 0000 00002 0ten
    0000 ... 0000 0000 0000 00012
    1ten0000 ... 0000 0000 0000 00102
    2ten. . .0111 ... 1111 1111 1111 11012
    2,147,483,645100111 ... 1111 1111 1111 11102
    2,147,483,646100111 ... 1111 1111 1111
    11112 2,147,483,647101000 ... 0000 0000
    0000 00002 2,147,483,648101000 ... 0000
    0000 0000 00012 2,147,483,647101000 ...
    0000 0000 0000 00102 2,147,483,64610. .
    . 1111 ... 1111 1111 1111 11012
    3101111 ... 1111 1111 1111 11102
    2101111 ... 1111 1111 1111 11112 110
  • Circular just keep adding one and itll
    naturally wrap around
  • First bit 0 on non-negative, 1 on negative,
    called sign bit
  • Drawback one negative with no positive
    2,147,483,64810

22
Twos Complement Formula (1/2)
  • Normally numbers are represented as follows
  • d31x231 d30x230 ... d2x22 d1x21 d0x20
  • However, we know sign bit is only 1 for
    negatives, so lets use this
  • d31x -231 d30x230 ... d2x22 d1x21 d0x20
  • Note that this means that, for twos complement
    numbers, we have to specify how many bits were
    working with, since highest-order bit is
    negative. In general, well use 32 bits.

23
Twos Complement Formula (2/2)
  • Example
  • Using Formula
  • 1111 1111 1111 1111 1111 1111 1111 11002
    1x-2311x2301x229...1x220x210x20 -231
    230 229 ... 22 0 0
    -2,147,483,64810 2,147,483,64410 -410
  • Invert and add one
  • 1111 1111 1111 1111 1111 1111 1111 11002 invert
    0000 0000 0011 add one 0000 0000 0100
    410 therefore, original value was -410

24
Signed v. Unsigned Numbers
  • C declaration int
  • declares a signed number
  • uses twos complement
  • C declaration unsigned int
  • declares an unsigned number
  • treats 32-bit number as an unsigned integer (e.g.
    the most significant bit is part of the number,
    not a sign bit)

25
Signed v. Unsigned Comparison
  • s0 has
  • 1111 1111 1111 1111 1111 1111 1111 11002
  • s1 has
  • 0011 1011 1001 1010 1000 1010 0000 00002
  • Signed Comparison
  • -410 lt 1,000,000,00010?
  • Unsigned Comparison
  • 4,294,967,29210 lt 1,000,000,00010?

26
Sign Extension (1/3)
  • For positive numbers
  • If we have an n-bit twos complement number, we
    can extend it to n1 bits by adding another 0 at
    the front
  • Example
  • 4-bit number 0010
  • becomes 5-bit number 00010
  • becomes 6-bit number 000010
  • This is logical we can add any number of leading
    0s to the front of a number without changing its
    value.

27
Sign Extension (2/3)
  • For negative numbers
  • If we have an n-bit twos complement number, we
    can extend it to n1 bits by adding another 1 at
    the front
  • Example
  • 4-bit number 1110
  • becomes 5-bit number 11110
  • becomes 6-bit number 111110
  • This is less intuitive we can add any number of
    leading 1s to the front of a negative twos
    complement number without changing its value.

28
Sign Extension (3/3)
  • So why can we add leading 1s?
  • Simplest answer We know that, to convert a
    negative number to its opposite, we invert and
    add 1. The number of leading 1s doesnt change
    this calculation.
  • General Rule
  • To sign extend a number (to increase the number
    of bits of a signed number), repeat the signed
    bit any number of times (0 for positive, 1 for
    negative).

29
Overflow (1/3)
  • Example (using 4-bit numbers)
  • 15 1111
  • 3 0011
  • 18 10010
  • But we dont have room for 5-bit solution, so the
    solution would be 0010, which is 2, which is
    wrong.

30
Overflow (2/3)
  • In general, adding two n-bit numbers can produce
    an (n1)-bit result.
  • Since computers use fixed, 32-bit integers, this
    is a problem.
  • Definition Overflow occurs when the result of an
    addition or subtraction is incorrect due to the
    fact that the result must fit inside 32 bits.
  • Note that overflow is not the same as carry out.

31
Overflow (3/3)
  • For unsigned numbers, this means adding two large
    numbers and getting a small result (as on the
    previous slide).
  • For signed numbers, overflow occurs when two
    positive numbers are added and we get a negative
    result, or vice versa.
  • Similar rules apply for overflow due to
    subtraction.

32
Bit Groupings
  • In the MIPS architecture
  • bit binary digit a 1 or a 0
  • nibble 4 bits (very rarely used)
  • byte 8 bits
  • word 4 bytes 32 bits
  • Become familiar with these.

33
Things to Remember
  • Use decimal to do calculations, use binary to
    understand computers, use hex to understand
    binary
  • Twos complement is used everywhere in computers
    theres no avoiding it.
  • Overflow Since numbers are infinite but
    computers are finite, errors can occur.
Write a Comment
User Comments (0)
About PowerShow.com