Bitwise Hacks

Project Euler Day

  • 5:00PM Next Tuesday

Shapes 2

Regular Polygons and Stars

How do we calculate them?

Answer: with math!!!

Verticies: Po = (1,0) P1 = (cos(i2pi/n), sin(i2pi/n)) // i is not imaginary; it's the loop variable.

Bitwise Hacks

See the PPT for more.

Basic Bitwise Operations

// left shift:          <<
// right shift:            >>
//unsigned right shift:    >>>
// bitwise and:            &
// bitwise or:         |
// bitwise not:            ~
// bitwise xor:            ^

Setting and clearing bits

  • setting a bit: LEFT SHIFT, OR
n = n | (1 << bitNum);
  • clearing a bit: LEFT SHIFT, NOT, AND
n = n & ~ (1 << bitNum);
  • getting a particular bit: RIGHT SHIFT, AND
bit = (n >> bitNum) & 0x1;