Forum Moderators: coopster

Message Too Old, No Replies

Bitwise "And" in an "IF"

More newbieness.

         

fischermx

9:08 pm on Jun 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I have something like this :

if ($x & $z)
{

}

(x$, $z are integers)

I understand that's a bitwise and what do they do, but I don't understand why they are not being compared to another variable!? This look like the kind of C++ stuff that drove me crazy ...
So, to what is that equivalent to? I mean, which other syntax to get the same effect?

mcibor

10:08 pm on Jun 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$a & $bAndBits that are set in both $a and $b are set.

So what are you trying to achieve with that if? It's not a comparison. You will get 0 or!0

2 & 1 = 0 (10b & 01b = 00b)
2 & 3 = 2 (10b & 11b = 10b)

Are you sure that this is the problem? Maybe it lays somewhere in the logic?

Write more
Best regards
Michal Cibor

fischermx

10:31 pm on Jun 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your help, I'm new to PHP and that's in a script I tried to understand :
if ($x & $z)
{
some stuff here.
}

Now that you say that will be 0 or!0, so you mean it is equivalent to :
if (($x & $z) = 0)
{

}

or to :

if (($x & $z)!= 0)
{

}

Which one?

Timotheos

11:01 pm on Jun 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You basically got it fischermx but watch your comparision operators. A single = is to assign a value while a double == is for comparison purposes. This can really screw you up if you don't understand that.

mcibor

12:17 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(true){}else{}

Therefore if is true only when ($a & $b)!= 0

Best regards
Michal Cibor