Please switch off your mobile phones - PowerPoint PPT Presentation

About This Presentation
Title:

Please switch off your mobile phones

Description:

General number representation in arbitrary base. An algorithm for converting between bases ... (this is 2's complement representation of the negative number) 7 ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 15
Provided by: iit1
Category:

less

Transcript and Presenter's Notes

Title: Please switch off your mobile phones


1
Please switch off your mobile phones
2
Data Representation
  • Instructor Mainak Chaudhuri
  • mainakc_at_cse.iitk.ac.in

3
Agenda
  • Recap
  • Talking to the computer
  • Numbers
  • Data types
  • Integer and long integer
  • Floating-point and double
  • Character and String
  • Boolean

4
Recap
  • Algorithms
  • Programs
  • Compilers executables (binary 0 and 1)
  • Operating systems
  • Central processing unit (CPU) and memory

5
Numbers
  • How does computer understand numbers?
  • Knows only two symbols
  • We are mostly familiar with decimal numbers
  • General number representation in arbitrary base
  • An algorithm for converting between bases
  • Special case base2 (binary)
  • Is there a decimal to binary converter inside the
    computer? Compiler does it
  • Negative numbers?
  • Two representation conventions sign-magnitude
    representation and 2s complement representation

6
2s complement
  • Steps involved in converting decimal to 2s
    complement
  • Decide the number of bits (should be at least
    2integer part of log2N)
  • Write the magnitude in binary and append zeros at
    the front to fill up the remaining bits (this is
    2s complement of the positive number)
  • Flip all bits (this is 1s complement of the
    positive number)
  • Add 1 to it (this is 2s complement
    representation of the negative number)

7
2s complement
  • 2s complement to decimal
  • Write down the polynomial expansion in base 2
  • Append a negative sign to the leading coefficient
  • Comparison of 2s complement and sign-magnitude
  • Range in sign-magnitude with n bits
  • Range in 2s complement with n bits
  • Benefits of 2s complement in binary arithmetic
    will discuss later
  • All computers and calculators use 2s complement
    representation

8
Variables
  • Anything that stores a value
  • Symbols made up of characters A-Z, a-z, 0-9, _,
    ,
  • Called identifiers
  • Must start with a letter or _ or
  • Examples dayOfTheWeek, day_of_the_week,
    dayoftheweek, _dayoftheweek, myname, myName,
  • Constants are not variables 7, 100, 2.5,
  • Variables are useful for holding non-constant
    values

9
Data types
  • Integer and Long integer
  • int and long
  • Called keywords will learn more keywords
  • Keywords cannot be used as identifiers
  • Example
  • int x
  • x is the variable name which we have declared as
    an integer
  • x is said to be of type integer
  • int must be written in small characters
  • This is called syntax of a language
  • Not following it properly leads to syntax errors

10
Data types
  • Floating-point and double
  • Used for representing non-integer numbers
  • Examples
  • float pi, _run_rate, Average_score
  • double e, interestRate
  • pi, _run_rate, Average_score, e, interestRate are
    variable names
  • Notice the comma separating the names

11
Why data types?
  • Why cant I just use a variable in computation?
  • Every variable must have a type
  • Why must it be declared to have a type?
  • Allocation in the memory (recall the scratchpad)
  • Every variable should get some space in the rough
    sheet otherwise how can you use it for
    computation?
  • Help the compiler decide how many bits should be
    reserved for a variable
  • Observation compiler must know the data type to
    size mapping

12
More data types
  • Character and String
  • Non-numeric variables
  • You may be surprised we will see non-numeric
    variables in computing
  • Examples
  • char orange
  • String something
  • orange and something are variable names
  • Note the syntax
  • char is used to store a single symbol e.g. f,
    2, , ,
  • String is used to store a sequence of symbols
    e.g. My name is Mainak.

13
Even more data types
  • Boolean
  • Can take two values only true or false
  • true and false are two boolean constants
  • You may be surprised to see this type
  • Cant I declare boolean variables as integers?

14
Constants
  • Integer
  • 1, 234, -56, 0,
  • Floating-point and double
  • 4.5, 56.789, 3.14, 2.71, 0.693, 4.5e3, 45000e-1
  • Character
  • a, _, , A, m, ,
  • String
  • Hi there, How are you?, This is esc101!!
  • Boolean
  • true, false
  • You can assign a constant value to a variable
    how? Will learn in next class
Write a Comment
User Comments (0)
About PowerShow.com