Re: Bitwise &
| From: | Stephan Buchholz | Date: | Tue, 20 Nov 2001 11:34:20 +0000 |
| Subject: | Re: Bitwise & | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-75223@lists.php.net to get a copy of this message | ||
probably priority issue. try:
if (($One & $Two) == $One)
- -
in your example this means:
((0100 & 1111) == 0100)
(0100 == 0100)
TRUE
...in other case 0100 & (1111 == 0100) is 0100 & 0 is always 0
hope this helps
Buchholz
"Fred" wrote
> From: "Fred" <fred.nospam.please@querymagic.com>
> Date: Mon, 19 Nov 2001 18:02:53 -0800
> Subject: [PHP] Bitwise &
>
> I have 17 boolean variables which need to be stored in a MySQL database. I
> created a SET Column to store each of the boolean values as bits in the
> column. I should be able to test for the truth of a particular variable by
> checking if the corresponding bit is set.
>
> For example, if the first four variables are set (i.e. Column = a,b,c,d)
> then the numeric value of the column is 15 or 1111. To test for c I would
> check to see if the third bit (4 or 100) is set.
>
> I chose to do the check this way:
>
> if ($One & $Two == $One) echo "True";
>
> This does not seem to work in many instances. Take the example above:
> 4 and 15 should equal 4
> 0100 and
> 1111 equals
> 0100
>
> For some reason, my script gives 0000 as the result of 0100 and 1111. It is
> fairly clear that this is not the correct behavior of the bitwise &
> operator.
>
> Oddly enough, in some instances is works as expected:
> 16 and 17 should equal 16
> 10000 and
> 10001 equals
> 10000
>
> My script has no problem generating the expected result here.
>
> Perhaps this problem arises from the loosely typed nature of PHP variables,
> but I do not see how it could. Any pointers would be appreciated.
>
> Fred