* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download ppt
Survey
Document related concepts
Transcript
Please switch off your mobile phones 1 Data Representation Instructor: Mainak Chaudhuri [email protected] 2 Agenda • Recap • Talking to the computer – Numbers • Data types – Integer and long integer – Floating-point and double – Character and String – Boolean 3 Recap • • • • • Algorithms Programs Compilers: executables (binary: 0 and 1) Operating systems Central processing unit (CPU) and memory 4 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 5 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) 6 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 signmagnitude – Range in sign-magnitude with n bits – Range in 2’s complement with n bits – Benefits of 2’s complement in binary arithmetic: makes logic design simpler – All computers and calculators use 2’s complement representation 7 Variables • Anything that stores a value – Symbols made up of characters: A-Z, a-z, 09, _, $, … – 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 8 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 9 – 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 10 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 11 type to size mapping More data types • Character and String – Non-numeric variables – You may be surprised: we will see nonnumeric 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 12 symbols e.g. “My name is Tintin.” 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? • Byte – 8-bit integer – Example: byte x; – Range of x? • Short – 16-bit integer – Example: short y; 13 Constants • Integer (int) 1, 234, -56, 0, … • Floating-point and double (float, double) 4.5, 56.789, 3.14, 2.71, 0.693, 4.5e3, 45000e-1 • Character (char) ‘a’, ‘_’, ‘ ‘, ‘A’, ‘m’, ‘$’, … • String (String) “Hi there”, “How are you?”, “This is esc101!!” • Boolean (boolean) true, false • You can assign a constant value to a 14 variable: how? Will learn in next class