A bitwise-and-bitwise OR operation example in PHP _php tips

Source: Internet
Author: User
Tags bitwise
Bitwise vs. is primarily a binary number operation.

The code is as follows:
Copy Code code as follows:

<?php
$a = 1;
$b = 2;
$c = $a ^b;
echo $c//3
?>

This is not a simple addition relationship.
Decimal 1 translates into binary 00000001
Decimal 2 translates into binary 00000010
bitwise ^ 00000011//Just the difference is 1 ^_^
And then
Copy Code code as follows:

<?php
$a = 1;
$b = 2;
echo $a & $c; 1
?>

Decimal 3 translates into binary 00000011
Decimal 1 translates into binary 00000001
Bitwise & 00000001//Is the same as the same number of single digits, or 0.
Finally, the following usage is introduced. It is meaningless to return a value after bitwise &. It is mainly used to determine whether $a exists in $c//permission usage is more.
Copy Code code as follows:

<?php
$my _privilege = 15; 1+2+4+8 has full permissions
$Pri = ';
$privilege _arr = Array (8=> ' increase ', 4=> ' delete ',2=> ' change ',1=> ' check ');
foreach ($privilege _arr as $k => $v) {
$k & $my _privilege && $Pri. = ' I have '. $v. ' Power <br> ';
}
Echo $Pri;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.