Tuesday, 16 February 2016

Bit twiddling tricks



  • Power of two: if (n & (n-1) == 0) then n is a power of two
  • Odd or even: if (n & 1 == 0) then n is even
  • Set nth bit of number i to 1:  i | (1 << n)
  • Set nth bit of number i to 0: i & ~(1 << n )

No comments:

Post a Comment

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning&#...