Today, a friend in the friend group asked about bitwise AND or. .
I have been working in PHP for 1 year, so I may not be familiar with this area. Here I would like to introduce this part to my novice friends.
Pressing is mainly for binary number operations.
Copy code The code is as follows:
$a = 1;
$b = 2;
$c = $a^b;
echo $c // 3
?>
This is not a simple additive relationship
Convert decimal 1 to binary 00000001
Convert decimal 2 to binary 00000010
Bitwise ^ > Then,
Copy code The code is as follows:
$a = 1;
$b = 2;
echo $a & $c; // 1
?>
Decimal 3 is converted to binary 00000011
Decimal 1 is converted to binary 00000001
Bitwise & 00000001 // That’s each If the number of digits is the same, it will not change, otherwise it will be counted as 0
Finally, let’s introduce the usage; it is meaningless to return the value after bitwise &. Mainly used to determine whether $a exists in $c // There are many permission usages.
Copy code The code is as follows:
$my_privilege = 15; // 1+2+ 4+8 has all permissions
$Pri = '';
$privilege_arr = array(8=>'Add', 4=>'Delete',2=>'Change',1=> ;'Check');
foreach($privilege_arr as $k =>$v){
$k & $my_privilege && $Pri .= 'I have the power of '.$v.'
}
echo $Pri;
?>
http://www.bkjia.com/PHPjc/327790.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327790.htmlTechArticleToday, a friend in the friend group asked a question about bitwise AND or. . I have been working in PHP for 1 year, so I may not be familiar with this part. Here I would like to introduce this part to my novice friends. According to the location, it is mainly for binary...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn