Analysis of PHP bitwise AND or (^ ,)

Source: Internet
Author: User
This article provides a detailed analysis of the bitwise AND or (^,) in PHP. For more information, see

This article provides a detailed analysis of the bitwise AND or (^,) in PHP. For more information, see

Today, a friend in the group asked a question about bitwise AND or ..
I have been working in PHP for a year. Maybe I am not familiar with this part. Here I will introduce this part to new users.
Operations on binary data are mainly performed by location.

The Code is as follows:


$ A = 1;
$ B = 2;
$ C = $ a ^ B;
Echo $ c // 3
?>


This is not a simple addition.
Convert decimal 1 to binary 00000001
Convert decimal 2 to binary 00000010
Bitwise ^ 00000011 // The values of different values are regarded as 1 ^
Then, server space, U.S. servers, website space,

The Code is as follows:


$ A = 1;
$ B = 2;
Echo $ a & $ c; // 1
?>


Convert decimal 3 to binary 00000011
Convert decimal 1 to binary 00000001
Bitwise & 00000001 // The value remains the same for each single digit. Otherwise, the value is 0.
Finally, we will introduce the usage; it makes no sense to return values after bitwise. It is mainly used to determine whether $ a exists in $ c. // There are many permission usage options.

The Code is as follows:


$ My_privilege = 15; // 1 + 2 + 4 + 8 have all Permissions
$ Pri = '';
$ Privilege_arr = array (8 => 'add', 4 => 'delete', 2 => 'change', 1 => 'query ');
Foreach ($ privilege_arr as $ k => $ v ){
$ K & $ my_privilege & $ Pri. = ''I have '. $ v.' Power
';
}
Echo $ Pri;
?>

Related Article

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.