CMPT 120 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CMPT 120

Description:

It depends on the base (or radix) 10110 = 10110 (wow! ... Radix Complement. Negative numbers are represented as the complement of the positive number ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 28
Provided by: johne78
Category:
Tags: cmpt | radix

less

Transcript and Presenter's Notes

Title: CMPT 120


1
CMPT 120
  • Representation of Integers and Strings

2
Week 3 Objectives
  • Understand the binary representation of integers
  • Convert decimal numbers to and from binary
  • Understand 2s complement notation for
    representing negative integers
  • Understand how strings are represented

3
Why Binary?
  • Computers represent data in binary
  • Binary is the base 2 number system
  • Numbers are composed of the two digits 0 and 1
  • In base 10 (decimal) numbers are composed of the
    ten digits 0 to 9
  • Computers use binary because 0 and 1 can be
    mapped to on and off

4
Computer Memory
  • We will often refer to main memory
  • By which we mean RAM or random-access memory
  • RAM can be read from and written to
  • Can view RAM as a (very long) sequence of bits
  • bit is short for binary digit

5
Measuring Memory
  • Modern computers have lots of memory
  • A typical 32-bit computer system might have a
    60Gb hard drive, 512Mb RAM, and a 128Mb video
    card
  • What does all this gobbledegook mean?
  • 8 bits is a byte
  • 32 (or 4 bytes) is a word

6
kilo, mega, giga
  • 1 kilobyte 1 kb 210 bytes 1,024 bytes
    8,192 bits
  • 1 megabyte 1 Mb 220 bytes 1,048,576 bytes
    8,388,608 bits
  • 1 gigabyte 1 Gb 230 bytes 1,073,741,824
    bytes 8,589,934,592 bits
  • Wow! Thats a lot of bits!
  • the human brain has around 1,000,000,000,000
    neurons

7
Memory Addresses
  • Every memory location has an address so that we
    can read or write the bits
  • These addresses refer to words (32 bits)
  • the first 32 bits might have address 0
  • the next 32, the address 1, and so on

8
Number Bases
  • We usually count in base 10 (decimal)
  • But we dont have to
  • We could use base 8, or 16, or 13, or 2 (binary)
  • What number does 101 represent?
  • It depends on the base (or radix)
  • 10110 10110 (wow!)
  • 1018 6510 (182 081 180 64 0 1
    65)
  • 10116 25710 (1162 0161 1160 256 0
    16 257)
  • 1012 510 (122 021 120 4 0 1 5)

9
Number Bases
  • So what does 12,34510 represent?
  • 1104 2103 3102 4101 5100
  • What about 12,34516 represent?
  • 1164 2163 3162 5161 5160
  • And what about 11,0012?
  • 124 123 022 021 120 25

10
Representing Unsigned Numbers
  • How large a positive number can be represented in
    32 bits?
  • 1111 1111 1111 1111 1111 1111 1111 1111
  • or 4,294,967,295

231 2,147,483,648
230 1,073,741,824
229 536,870,912
11
Adding Unsigned Numbers
  • Binary addition can be performed in the same way
    as decimal addition
  • Remember 12 12 102
  • and 12 12 12 112
  • But how do we perform subtraction, and how do we
    represent negative numbers?

12
Representing Signed Numbers
  • Remember that the only unit of storage is bits
  • So the fact that a number is negative has to
    somehow be represented as a bit value
  • How would you do it?

13
Signed Integer Representation
  • Keep one bit (the left-most) to record the sign
  • 0 means and 1 means
  • But this is not a good representation
  • It wastes a bit (the sign bit) and
  • Has two representations of zero
  • It makes implementing subtraction difficult and
  • For reasons related to hardware efficiency we
    would like to avoid performing subtraction
    entirely

14
Radix Complement
  • Negative numbers are represented as the
    complement of the positive number
  • The complement of a number, N, in n digit base b
    arithmetic is bn N. In base 10 e.g.
  • complement of 24 is 102 24 76 (2 digit
    arithmetic)
  • complement of 024 is 103 24 976 (3 digit
    arithmetic)

15
Complement Subtraction
  • Complements can be used to do subtraction
  • Instead of subtracting a number add its
    complement and ignore any number past the fixed
    size (number of digits)
  • For example to calculate 52 24
  • 52 complement(24) or 52 76 (1)28

16
Huh!
  • What did we just do?
  • Remember the complement of 24 in 2 digit
    arithmetic is 100 24 76
  • Also remember that we will ignore the highest
    order digit
  • So in effect 52 complement(24) equals
  • 52 (100 24) 100 or
  • 52 24

17
But
  • So it looks like we can perform subtraction by
    just doing addition (using the complement)
  • But it seems like there is a catch here what is
    it?
  • To get the (base 10) complement we had to do
    subtraction!
  • DOH! (or maybe not)

18
2s Complement
  • In binary we can calculate the complement by
  • Padding the number with 0s up to n digits
  • Flipping all of the digits
  • Adding 1
  • Find the 2s complement of 01102 (610) and check
    that it is correct (using 4 digit arithmetic)
  • Note no subtraction used!

19
2s Complement Example
  • Calculate 6 2 in binary using 2s complement in
    4 digit arithmetic
  • The answer should be 4, or 0100 in binary
  • Remember that a number has to be padded with
    zeros up to the number of digits (4 in this case)
    before flipping the bits

20
32 bit 2s Complement
  • 32 bits is 2 31 bits so we use can use 31 - 1
    bits for the ve numbers, 31 bits for the ve
    numbers and 1 bit for zero
  • 231 1 positive numbers
  • 231 negative numbers
  • and 0
  • A range of 2,147,483,647 to 2,147,483,648

21
2s Complement Addition
  • To add x and y
  • If x or y is negative calculate its 2s
    complement
  • Add the two numbers
  • Check for overflow if both numbers have the same
    sign but the result is a different sign there is
    overflow and the resulting number is too big to
    be represented
  • For subtraction just add the 2s complement of
    the number to be subtracted

22
Python Integers
  • Python uses 2s complement for all integers less
    than 232
  • For larger integers it uses a special format that
    can represent any integer (as long as it fits in
    memory)
  • These larger integers are identified by a
    trailing L

23
String Representation
  • A string is a sequence of characters
  • How many characters do we need to represent?
  • A to Z, a to z, 0 to 9, and
  • !_at_()-_\lt,gt./?
  • So how many bits do we need?
  • 26 26 31 83

24
Letter Codes
  • Each character can be given a different value if
    represented in an 8 bit code
  • so 28 or 256 possible codes
  • This code is called ASCII
  • American Standard Code for Information
    Interchange
  • e.g. m 109, D 68, 0 111
  • find these with the Python ord function
  • e.g. ord('m')

25
Strings
  • To represent a string just use a sequence made up
    of the codes for each character
  • So the string representing Doom would be
  • 01000100 01101111 01101111 01101101
  • 010001002 6810 which represents D
  • 011011112 11110 which represents o
  • 011011112 11110 which represents o
  • 01101101 2 10910 which represents m

26
Types
  • Remember this number from the last slide?
  • 01000100 01101111 01101111 01101101
  • That represented Doom or did it?
  • Maybe it represents 1,148,153,709
  • Python need to keep track of types to know what
    the bits actually represent

27
Unicode
  • Unicode is an alternative to ASCII
  • It can represent thousands of symbols
  • This allows characters from other alphabets to be
    represented
  • To create a Unicode string in Python put u in
    front of a string
  • u'This is a Unicode string'
Write a Comment
User Comments (0)
About PowerShow.com