140 likes | 156 Views
Please switch off your mobile phones. Data Representation. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Agenda. Recap Talking to the computer Numbers Data types Integer and long integer Floating-point and double Character and String Boolean. Recap. Algorithms Programs
E N D
Data Representation Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
Agenda • Recap • Talking to the computer • Numbers • Data types • Integer and long integer • Floating-point and double • Character and String • Boolean
Recap • Algorithms • Programs • Compilers: executables (binary: 0 and 1) • Operating systems • Central processing unit (CPU) and memory
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: base=2 (binary) • Is there a decimal to binary converter inside the computer? Compiler does it • Negative numbers? • Two representation conventions: sign-magnitude representation and 2’s complement representation
2’s complement • Steps involved in converting decimal to 2’s complement • Decide the number of bits (should be at least 2+integer part of log2|N|) • Write the magnitude in binary and append zeros at the front to fill up the remaining bits (this is 2’s complement of the positive number) • Flip all bits (this is 1’s complement of the positive number) • Add 1 to it (this is 2’s complement representation of the negative number)
2’s complement • 2’s complement to decimal: • Write down the polynomial expansion in base 2 • Append a negative sign to the leading coefficient • Comparison of 2’s complement and sign-magnitude • Range in sign-magnitude with n bits • Range in 2’s complement with n bits • Benefits of 2’s complement in binary arithmetic: will discuss later • All computers and calculators use 2’s complement representation
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
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
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
Why data types? • Why can’t 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
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.”
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 • Can’t I declare boolean variables as integers?
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