note 35318 added to ref.math
| From: | jordanolsommer at imap dot cc | Date: | Wed, 27 Aug 2003 13:07:24 +0000 |
| Subject: | note 35318 added to ref.math | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55175@lists.php.net to get a copy of this message | ||
The reason the bitwise AND ("&") operator works to determine whether a number is odd
or even is because odd numbers expressed in binary always have the rightmost (2^0) bit = 1 and even
numbers always have the 2^0 bit = 0.
So if you do a " 1 & $num", it will return zero if the number is even (since xxxxxxx0
[the even number in binary] and 00000001 [the 1]) don't share any bits, and will return 1 if
the number is odd (xxxxxx1 and 000001).
a clever way of doing things, but $num % 2 would work as well i think :).
----
Manual Page -- http://www.php.net/manual/en/ref.math.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35318
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35318&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35318&report=yes