Bitwise VS, Bitwise XOR, bitwise inverse

Source: Internet
Author: User
Tags bitwise

**& bitwise AND, same as unchanged, otherwise all counted as 0

| Bitwise OR,
^ Bitwise XOR, not the same is counted as 1**
PHP bitwise AND OR (^, &) operation is also a very common logic to determine the type, there are many new PHP novice may not be familiar with this, today combined with some code on the PHP and or operations to do some introduction, first of all, in PHP, the bitwise and mainly for the binary number operation:

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

Decimal 1 is converted into binary: 00000001
Decimal 2 is converted into binary: 00000010
The bitwise ^ 00000011, is the difference is counted as 1, and then:

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

Decimal 3 is converted into binary: 00000011
Decimal 1 is converted into binary: 00000001
Bitwise & 00000001, is the same single-digit constant, otherwise all counted as 0, the bitwise "&" After the return value is meaningless, is mainly used to determine whether a $ A is present in the $c, permission usage is more:

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

A number of fields can be distinguished by a single value
> $a = Hexdec (' 0x10c04000 ');
> $b = ($a & 0x0ff00000) >20;
> $c = ($a & 0x000ff000) >12;
> var_dump ($a, $b, $c);
> Output:
> int 281034752
> int 12
> int 4

Bitwise VS, Bitwise XOR, bitwise inverse

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.