views:

97

answers:

2

Hi

I am wondering what other bitwise and logical operations you've used that saved your day.

For example, my last great time (and resources') saver has been

if(!((A^B) & B))

reads: if A has at least B's access rights, where rights were saved in the bit fields A and B.

Please use the classical operators: binary & (and) | (or), ^ (xor), ~ (invert) and the logical ones && (and), || (or), ! (not).

+5  A: 

Mandatory link: http://graphics.stanford.edu/~seander/bithacks.html.

Oli Charlesworth
Love that page! I originally found it looking for a quick way to get the integer part of the log (base 2) of an integer
Cameron
thanks for this link
dfens
A: 

double negation rules: !! value result in 1 or 0.

aaa
It wasn't my downvote, but I would always write `(0 != x)`.
Oli Charlesworth