Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 5
    if ($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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    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

    To your example:

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

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

  • #4
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by crmpicco View Post
    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.
    Last edited by Fou-Lu; 09-24-2009 at 06:40 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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