Home > Backend Development > PHP Tutorial > PHP bitwise AND (&) operator application practice

PHP bitwise AND (&) operator application practice

WBOY
Release: 2016-07-29 09:12:36
Original
1423 people have browsed it

PHP’s bitwise AND operation is to convert numbers into binary format for bit operations.

The explanation in the PHP manual is

$a & $b And (bitwise AND) will set the bits in $a and $b that are both 1 to 1.

1. Operation method of & (parity judgment)

Perform a bitwise AND operation on an integer and "1". The operation result is "0", which means it is an even number, and the operation result is "1", which means it is an odd number.

$a = 3&1;
echo '3&1:'.$a ; //<span style="font-family: Simsun;font-size:14px;">3&1:1</span>
echo '<br>';
$b = 4&1;
echo '4&1:'.$b; //<span style="font-family: Simsun;font-size:14px;">4&1:0</span>
Copy after login
3 and 1 are converted to binary to

'3' binary: 11
'1' binary: 01 Then the binary operation result of 3&1 is 01, and converted to decimal is '1';

'4' binary: 100

'1' binary: 001 The binary operation result of 4&1 is 000, and converted to decimal is '0';

PS:'%' (modulo operation) symbol can also be used to calculate parity, "3% The output result of "2" is 1, and the output result of "4%2" is 0. The operation efficiency of "%" is slightly higher than that of "&" operator.

function getMillisecond() {
    list($t1, $t2) = explode(' ', microtime());
    return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
/*
    判断数字的奇偶性
*/
$a = getMillisecond();
for($i=1;$i<10000000;$i++)
 $i&1;
$b = getMillisecond();
echo '&执行1千万次计算毫秒耗时:'.($b-$a); //&执行1千万次计算毫秒耗时:1068
echo '<br>';
$c = getMillisecond();
for($i=1;$i<10000000;$i++)
 $i%2;
$d = getMillisecond();
echo &#39;%执行1千万次计算毫秒耗时:&#39;.($d-$c); //%执行1千万次计算毫秒耗时:1035
Copy after login

2. Judgment of user permissions

Assume that the user permission allocation module in a system sets the permissions to 1=>view, 2=>add, 4=>modify, 8=>delete, so To store user permissions, you only need to store an integer in the database, and there is no need to store separated strings.

If the user has add and view permissions, the function code is: 1+2 = 3; if the user has all permissions, the function code is: 1+2+4+8 = 15;

All the user permissions are 12

Check whether the user permission has modification permission: 12&4. The result is 4, which means it has modification permission.

Check whether the user has new permissions: 12&2 The result is 0, which means there are no new permissions.

Find all users with modification permissions in the database: select * from user where (user_mod&4) > 0 to find out all users with modification permissions.

The above introduces the application practice of PHP bitwise AND (&) operator, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template