Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: matching value using a bitwise operator

1. ## matching value using a bitwise operator

I'm new to bitwise operators and the PHP documentation isn't helping my for the code I have below. In the example below \$ulogindetails["securityvalue"] is 34.

PHP Code:
``` if (\$ulogindetails["securityvalue"] & 880) {   ... do sumthin ...   }  ```
Can anyone shed light on how this is working? When would the value match to 880?

Cheers,
Picco

• AND will return the common bits between the two. 880 & 34 share the 32'nd bit, so it will result in a valid 32.

I get the impression that 880 is supposed to be a singular item, which is not possible, 880 = 512 + 256 + 64 + 32 + 16
While the 34 is 32 + 2, sharing the common 32.

To check if it matches 880 using bitwise (which doesn't make any sense since the bit check is using 880 so a simple comparison will work) you can use `if (880 == (\$myVar & 880))`
The idea behind bitwise permissions is to take a less aggressive approach to it:
PHP Code:
``` define('CAN_READ', 1);define('CAN_EDIT', 2);define('CAN_DELETE', 4);// Do stuff for our 'user', we'll give them privs of 5if (\$userPerms & CAN_READ){    // Our user can read}if (\$userPerms & CAN_EDIT){    // Our user can edit (this user cannot, since 5 & 2 is 0)}if (\$userPerms & CAN_DELETE){   // Our user can delete}  ```
Does that make sense?

• When you think bitwise, think binary.

Code:
```34  = 0000000000100010
880 = 1000101101110000```
In a bitwise AND, if and only if both values are 1, the result will be 1. Anything else is zero. So:

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

In a bitwise OR, if any of the values are one, the result will be one. So:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

Code:
```34  = 0000000000100010   AND
880 = 1000101101110000
--------------------------
0000000000100000```
Extended a bit:

Code:
```34  = 0000000000100010   OR
880 = 1000101101110000
--------------------------
1000101101110010```

• Thanks for those explanations, I have been reading through them, but i'm still slightly confused as to when the if statement would evaluate and would be true? How would we get into '... do sumthin ...' ?

PHP Code:
``` if (\$ulogindetails["securityvalue"] & 880) {  ... do sumthin ...  }  ```
Cheers,
Picco

• Originally Posted by crmpicco
Thanks for those explanations, I have been reading through them, but i'm still slightly confused as to when the if statement would evaluate and would be true? How would we get into '... do sumthin ...' ?

PHP Code:
``` if (\$ulogindetails["securityvalue"] & 880) {   ... do sumthin ...   }  ```
Cheers,
Picco
You can't really in this situation. 880 does not represent a valid (singular) bit value, so it can only be comparable by the use of equality (==) or for the use of contained bits.
880 contains the following bit values: 512 + 256 + 64 + 32 + 16. That means as long as the 'securityvalue' contains a number that includes one or more of the above bits (18 for example would match 16 + 2) it will be deemed true.

Edit:
Lol, my statement doesn't really make sense to you're question. The answer is, it will be false when a comparable number does not include one or more of the following bits: 512, 256, 64, 32, 16. It is true when containing one or more of the previous bits.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•