Bit-wise & operaetor is equivalent to a modulus and a division
operation. Using simplicity I’ll use zero-padded binary numerals:
00 & 10 == 00
01 & 10 == 00
10 & 10 == 10
11 & 10 == 10
This operation and operand (&10) is the same as using integer division
to “shift off” the lower binary digits (or “bits”) and, assuming we
the only bits we have left are the ones we want, adjusting the
remaining bit back into place with multiplication:
00 / 10 * 10 == 00
01 / 10 * 10 == 00
10 / 10 * 10 == 10
11 / 10 * 10 == 10
If we have more digits, modulus is required, but could have been used
above anyway:
000 / 010 % 001 * 010 == 000
001 / 010 % 001 * 010 == 000
010 / 010 % 001 * 010 == 010
011 / 010 % 001 * 010 == 010
100 / 010 % 001 * 010 == 000
101 / 010 % 001 * 010 == 000
110 / 010 % 001 * 010 == 010
111 / 010 % 001 * 010 == 010
Learn the following identities. I shall use two asterisks (**) to
signify exponentiation, as the carot (^) in C is the XOR operator.
Let p be any positive integer:
(x/(2p)%(20)*(2**p)) == (x&p)
So, for any positive integer power of 2 “n”:
(x/n%1*n) == (x&p)
Of course, these identities only describe the & operator when one of
the operands is a power of two. You can have more complex stuff like
this:
11111111 & 11111111 == 11111111
11111111 & 00000000 == 00000000
11111111 & 00011000 == 00011000
00001111 & 01010101 == 00000101
10101010 & 01010101 == 00000000
Have funOn Mon, Jan 18, 2010 at 10:33 AM, dekyco wrote:
if ( timer_milliseconds & 1024 ) else
Did you mean something like modulus:
–
http://codebad.com/