Home > Backend Development > PHP Tutorial > Different uses of bitwise operations (bitwise AND, bitwise OR) in PHP

Different uses of bitwise operations (bitwise AND, bitwise OR) in PHP

WBOY
Release: 2016-07-25 09:12:12
Original
1255 people have browsed it
What is the role of bitwise AND and bitwise OR in the development process of PHP?
  1. /**
  2. * 1. Permission application
  3. * For which permissions you have, add up the values ​​corresponding to these permissions
  4. * For example: if the moderator has permissions (add, delete, modify, query), then the moderator's permission value is stored as 15 (8 +4+2+1)
  5. * Then compare [sum of permission values] with [actual permission value] [located]
  6. * If the result is true, you have permission
  7. * If the result is false, you have no permission
  8. *
  9. * Note: permissions The value must be the Nth power of 2, starting from the 0th power. The 31st power is 2147483648
  10. * The 32nd power is 4294967296, which has exceeded the common int (10) maximum storage of 4294967295, so you must pay attention to the number of permissions (<31 )
  11. * Of course, if the storage format is bitint or varchar, which can store longer numbers, then the number of permissions can continue to increase
  12. */
  13. $permission = 15; //1+2+4+8 has all permissions
  14. $permissions = array(
  15. 8 => 'Add',
  16. 4 => 'Delete',
  17. 2 => 'Modify',
  18. 1 => 'Query'
  19. );
  20. foreach ($permissions as $key => $val) {
  21. if($key & $permission) {
  22. echo 'I have the power of ' . $val . '
    ';
  23. }
  24. }
Copy code
  1. Waiting to be added
Copy code


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