Home >Backend Development >PHP Tutorial >Examples of bitwise AND and bitwise OR operations in PHP_PHP Tutorial

Examples of bitwise AND and bitwise OR operations in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:48987browse

Bitwise AND is mainly an operation on binary numbers.

The code is as follows:

Copy code The code is as follows:

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

This is not simple Addition relationship
Decimal 1 is converted to binary 00000001
Decimal 2 is converted to binary 00000010
Bitwise^ 00000011 // Even if they are not the same, they are all counted as 1^_^
Then,
Copy code The code is as follows:

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

Decimal 3 converted to binary 00000011
Decimal 1 converted to binary 00000001
Bitwise & 00000001 / / means that all digits are the same, otherwise they are 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;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/728104.htmlTechArticleBitwise AND is mainly an operation on binary numbers. The code is as follows: Copy the code as follows: ?php $a = 1; $b = 2; $c = $a^b; echo $c // 3 ? This is not a simple addition relationship. Decimal 1 is converted into...
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