Lecture 003

Machine Words

modern comuputer: k=32 bits parallel (but they handle integers as if they were 32) int: always 32 bits long (00000000000000000000000000100101)

Modulo Ring

Modulo Ring

Modulo Arithmetics

Modulo Arithmetics

Modulo Arithmetics for Negative

Modulo Arithmetics for Negative

Solving represent positive and negative

Two's Complement

Two's Complement

string foo(int x) {
  int z = i+x; //c0 always compute mod 32 at the end of each calculation.
  if (x+1==z) return "Good";
  else return "Bad";
}
string bar(int x) {
  if(x+1>x) return "Good";
  else return "Strange";
}

Safe Operations in Proofing Considering Overflow (See

Here)

More About Mod Operation

Here

Division and Modulus

Division by Zero: :warning: division need preconditions!

// in x/y: we requires
//@requires y!=0;
//@requires !(x==int_min() && y==-1);

Color and Bitwise Operations

Bitwise

Bitwise
color: max=0~255=2^8 bitwise: int -> int (32 bits) boolean: c0 does not evaluate int as boolean double && and ||: bool -> bool

// alpha, red, green, blue
int color = c & 0x00FF00FF //write FF to preserve and 00 to delete
int opacity = c | 0xFF000000

XOR

return 0 when two bits are the same and one when not the same

Shifts

precondition: //@requires 0 <= k && k<32 (there is no inverse or overflow shifting)

//@requires 0 <= k && k<32
x << k //left shift by k bit, overflow dropped, new bits replaced by 0
x >> k //right shift by k bit, overflow dropped new bits are the copy of leftmost bit

:warning: left and right shift behaves differently

Shifts

Shifts

Shifts as Arithmetics

Warnings

Warnings


written 02 graded programming 01 due Thursday 9pm

Table of Content