230 likes | 422 Views
WELCOME To ESC101N: Fundamentals of Computing. Instructor: Ajai Jain ajain@cse.iitk.ac.in. Lecture hours Monday, Wednesday, Thursday: 8-9am, L17 Please come to class in time Labs 2pm-5pm, 11 lab sessions No lab this week Tutorial Tuesday 8-9 am, Tutorial Block 101-110
E N D
WELCOME To ESC101N:Fundamentals of Computing Instructor: Ajai Jain ajain@cse.iitk.ac.in
Lecture hours • Monday, Wednesday, Thursday: 8-9am, L17 • Please come to class in time • Labs • 2pm-5pm, 11 lab sessions • No lab this week • Tutorial • Tuesday 8-9 am, Tutorial Block 101-110 • No tutorial this week
Administrivia • Grading - Exam: 15+15+30 • Project and/or lab test :20 • Weekly lab sessions: 10 • Two surprise quizzes: 10 • Held in tutorial sessions • No make up for surprise quizzes (so come to tutorial regularly)
There will be a course web page with all info • Text book • Nothing specific: your choice • Suggestion: “Java Elements: Principles of Programming in Java” by Bailey and Bailey • More references are on the webpage • Visit past course sites: www.iitk.ac.in/esc101
What this course is not about • This is NOT a course on Java • You will learn how to solve problems with computers: especially the ones that you cannot solve with paper and pencil quickly • The greater part of the lectures will be devoted to the concepts involved in developing a computer algorithm • Sequence of steps that solve a problem • Java will be used as a vehicle to demonstrate the concepts • Do not expect to become an expert in Java after taking this course
Anatomy of a computer • What you see • A monitor, a keyboard, a mouse, a printer … • Input/Output devices • Through these you ask the computer to do something and the computer tells you the results • Need a way to convey your commands to the computer (it is really a stupid device which cannot do anything on its own) • Internally • A central processing unit and a scratchpad (often called main memory) accomplish the job
Anatomy of a computer • Central processing unit does not understand English, not even Java • It only understands two symbols: 0 and 1 • These are called bits (short for binary digits) • You encode your algorithm into a high-level language called Java • This is called a program • This is harder to understand than English, but easier to understand than a 0-1 encoding • How do I encode a program in 0-1? This is used only for storing the program in main memory
Anatomy of a computer • A system software compiler translates the program into a binary encoding called an object program • This is almost understandable to the central processing unit (often called a microprocessor) • Another software called a linker adds something more to an object program to convert it to an executable • This is understandable to the CPU • But somehow it needs to get started executing
Anatomy of a computer • An operating system loads the executable in main memory and hands over the control to the CPU • Now the CPU starts executing your program (essentially the binary executable) • Once in a while it prints something on the monitor and you appreciate that • Notice that it is not doing anything on its own, only doing whatever you have asked it to do • At some point the CPU completes the execution and you have all the results
A simple program • Let’s write a program in English (almost) • Want to add five numbers a, b, c, d, e and print the result on monitor print (monitor, a+b+c+d+e) • print is used as a function which takes two arguments: where to print and what to print • A binary translation of this could convert each character i.e. p, r, i, n, t, (, m, … into a binary string e.g., p is the 16th letter of alphabet, so represent it as 16 zeros; put a 1 to mark the end of a character • Now I can design a CPU which can understand this translation and execute my program (caution: this is just an example)
“The Computing Stack” ESC101N HLL=Java Problem Algorithm Program (HLLs) HLL Compiler/Linker Executable binary Operating System Microarchitecture Circuits Transistors Central in CS Hardware/ software interface Hardware
Data Representation • 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: makes logic design simpler • 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 • Every variable should get some space in thememory; 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 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 • Byte • 8-bit integer • Example: byte x; • Range of x? • Short • 16-bit integer • Example: short y;
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