Analysis of PHP bitwise and or (^, & amp ;). 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. I am mainly talking about the bitwise AND or problems in the group of friends today. ..
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,
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;
?>
The bitwise and or of the merge statement .. 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. This part is mainly for the second step...