CSE 12 Week Three Lecture One Tomorrow s discussion: Getting started on hw4 Finishing content from last Tuesday Ex in 4 bits, signed addition: 1 bit of sign, 3 bits of magnitude 0101: 5 + 0110: +6 ------- -- Problem: Overflow a bit of magnitude or. 0100: flip + 1: add one ------- Observation: overflow occurred: o o o When a computer program produces overflow what happens? Ans: Ramifications:.
Bit manipulation: shifting and masking What: operations on ints/longs/chars that perform bit-by-bit comparisons, result based on the operation performed. Where: C, C++, Java Masking: bit-by-bit comparison, only a portion of the source shows through. NOT ~ ~0 => 1, ~1 => 0 Bits in action: src: 1010 1010 ~src: Example in code: dst = Intuition: Use: AND: & 1&1 => 1, else => 0 Bits in action: src: 1010 1010 &mask: 1111 0000 dst: Example in code: dst = ; Intuition: ANDing w/ 1s =>, ANDing w/0s => Use:, Practical Use: Extract one hexadecimal digit from a number in a hexout method: digit = number & 0x????????; // extracts last digit number: &mask: digit: 0x12345678 0x????????? => digit extracted
OR 0 0 => 0, else 1 Bits in action: scr: 1010 1010 mask: 1111 0000 dst: Intuition: ORing w/ 1s =>, ORing w/0s => Use: Practical Use: Convert a decimal digit to ASCII: code = digit 0 ; // 7 0x30 => 0x37 7 scr: 0000 0111 mask:?????????? dst: => ASCII code for 7 XOR ^ 0^1 => 1, 1^0=> 1, else 0 Bits in action: scr: 1010 1010 ^mask: 1111 0000 dst: Intuition: XORing w/ 1s =>, XORing w/0s => Practical Use: - - - puzzle: xxx ^= yyy; yyy^=xxx; xxx^=yyy;
Shifting: Source value shifting by an amount in bits >> right shift << left shift Ex: 3: 6: // 3 shifted by bit to the 12: // 3 shifted by to the 24: // 3 shifted by to the 48: // 3 shifted by to the Shifting left by powers of, Shifting right by powers of Ex: in decimal: 123 x 100 = 12,300 Ternary operator: if-then-else expression (condition)? true_value : false value Ex: aaa = (bbb == 10)? 20 : 30; // same as: if ( ) aaa = ; else aaa = ; Reversing stack1 onto stack2: while ( ) { ; ; ; }
How to declare and initialize an array: int array[] = {1,2,3,4,5,6,7}; Parallel arrays: What: Two (or more) arrays that share the same : For us:, Compiler Behavior - What is a compiler? - Ans: o o - What do we want a compiler to do for us? - Ans: o o Compiler errors: 1) Lexical errors: Input is not recognized as legal in terms of basic symbols. - First errors the compiler identifies a. b. c. d. English: 2) Parse errors or Syntax errors: Group of tokens is legal, but the order is unexpected. a. b. English:
3) Semantic error: Structure is legal, but your statement doesn t make sense. a. b. c. d. English: 4) Warning: The compiler will do as you ask, but not not necessarily as you want. - in CSE 12, we will compile without warnings a. b. c. d. e. English: 5) Advice: All errors beyond the first are based on the compiler making a guess at what your meant: a. Guess was good: Error message is. b. Guess was wrong: Error message is. c. Therefore correct the first few messages and compile again d. Therefore don t even look at 20 th message Run-Time Errors: What: Your program asked to do the impossible. Ex: if (10/0) - access memory that is out of range. - result: in C/C++, in Java - The core file: o A run-time image of your program in execution just before you asked to do the impossible. o Often very large files (consume entire quota) o What good is it? The can read it o gdb a.out core o where tell the line of problem.
Side note: Desire to remove core file, xxx.o: student types (note the extra unwanted space after the * before the.o: rm core *.o * is expanded by UNIX to match all files in current directory, and they will all be removed. make clean // safely remove core, exe,.class,.o make backup // copies source into a backup directory Logic Errors: The program does what you ask, not what you want. - Most of your time will be spent here. Ex: Output is wrong. Ex: infinite loop. Ex: extra output of display.
CSE 12 Week Three Discussion Two intopost example: 00 2 4 2 6 4 // priorities ((27 + 452) * 2 16 ^ 2) / 7 <ret> What: What: stack 1 stack 2 eval example: What: What: stack 1 stack 2 To represent a calculator word for an operator, you need all of the following all together in one long (word): 1. : to distinguish an operator from a number 2. : -> to display to the user
3. : -> used in intopost logic 4. : -> which function to execute Inside eval, you will use the functions array to evaluate each part of the expression. long eval (.) { long op1, op2, word;// for the operands and operator result = ; // word -> originated from a stack it s the operator } You are NOT going to have a switch/case statement switch (operator) { case + : NO!!! case - : NO!!! } ungetting characters: What: intopost and decin will need to put characters back into when they read a character not processed directly by the function. Why: So that the character can be processed by the processing function. For example, must read the entire number. When: - decin (provided) reads a for to process. - intopost (you do) reads a for to process.
Obtaining the priority from the index: index Symbol 0 ( 1 ) 2 + 3-4 * 5 / 6 ^ 7 blank 8!. The setupword function: What: Your constructor function to create the long to be used as the operator to be able to store on your stacks - the calculator word. How: + results in: binary:
hexadecimal: To get started with some code: long setupword (char character) { } Note: are magic numbers - grader takes off Better: #define #define Accessor methods on the calculator word: What:,, extract what creates. Example use of can be found in eval notes above. Please keep in mind: Only the following goes on the stacks in intopost : - return values from: o o
o NO EXCEPTIONS!!! If you forget, you will have errors!!!