[RESOLVED] PHP: IF-statments: One ampersand

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

[RESOLVED] PHP: IF-statments: One ampersand

Post by spider.nick »

I need some help wrapping my head around this. I did not write this code, so I am trying to understand why the User Access Control [UAC] is working the way it is.

When the check

Code: Select all

if( 1 & 16 )
is run, it returns false. When

Code: Select all

if( 1 & 127 )
is run, it returns true.

Any help is greatly appreciated.

Nick
Last edited by spider.nick on Wed Jul 15, 2009 12:47 pm, edited 1 time in total.
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: PHP: IF-statments: One ampersand

Post by turbolemon »

It's all about bitwise operators:

Code: Select all

$a & $b
And
Bits that are set in both $a and $b are set.
If you know binary, 1 & 127 is true because 00000001 = binary 1 and 01111111 = binary 127, and all the set bits (8th 1) are set in binary 127, whereas binary 16 = 00010000, therefore the 8th bit set in binary 1 is not set in binary 16. A quite efficient way to store this information, a single byte :).
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: PHP: IF-statments: One ampersand

Post by spider.nick »

turbolemon wrote:It's all about bitwise operators:

Code: Select all

$a & $b
And
Bits that are set in both $a and $b are set.
If you know binary, 1 & 127 is true because 00000001 = binary 1 and 01111111 = binary 127, and all the set bits (8th 1) are set in binary 127, whereas binary 16 = 00010000, therefore the 8th bit set in binary 1 is not set in binary 16. A quite efficient way to store this information, a single byte :).
I was trying to comprehend it as binary; however, I was having issues pin-pointing exactly how it was being figured. Thank you for the prompt explanation!

Nick
Post Reply