PHP bitwise AND (&) operator application practice

Source: Internet
Author: User
: This article mainly introduces the application of the bitwise AND (&) operators in PHP. if you are interested in the PHP Tutorial, refer to it. In PHP, bitwise AND operations convert numbers into binary formats for bitwise operations.

The instructions in the PHP Manual are as follows:

$ A & $ B And (bitwise And) Set the bits of $ a and $ B to 1.

1. & calculation method (parity judgment)

Bitwise AND operation is performed on integers and "1". "0" indicates an even number, and "1" indicates an odd number.

$a = 3&1;echo '3&1:'.$a ; //3&1:1echo '
';$b = 4&1;echo '4&1:'.$b; //4&1:0
3 and 1 are converted to binary

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

'4' binary: 100

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

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

Function getMillisecond () {list ($ t1, $ t2) = explode ('', microtime (); return (float) sprintf ('%. 0f', (floatval ($ t1) + floatval ($ t2) * 1000);}/* judge the parity of a number */$ a = getMillisecond (); for ($ I = 1; $ I <10000000; $ I ++) $ I & 1; $ B = getMillisecond (); echo '& tens of millions of computations executed in milliseconds: '. ($ B-$ a); // execute 10 million computation times in milliseconds: 1068 echo'
'; $ C = getMillisecond (); for ($ I = 1; $ I <10000000; $ I ++) $ I % 2; $ d = getMillisecond (); echo '% execution of tens of millions of computation time in milliseconds :'. ($ d-$ c); // % execution of 10 million computation time in milliseconds: 1035

2. user permission judgment

Assume that the user permission assignment module in a system sets the permission to 1 => view, 2 => add, 4 => Modify, 8 => delete, so that the user permissions are stored, you only need to store an integer in the database, instead of storing the separated strings.

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

All permissions used by the user are 12

Check whether the user has the modification permission. the result of 12 & 4 is 4, indicating that the user has the modification permission.

Check whether the user has the new permission. the result of 12 & 2 is 0, indicating that no new permission is granted.

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

The above introduces the PHP bitwise AND (&) operator application practices, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.