Practical Use of bitwise OR and bitwise in projects

Source: Internet
Author: User

When I was engaged in PHP, I found that the | and & operators are very clever. I would like to share with you this article.

Bitwise operator | and & Simplified checkbox check box access value. Set the name attribute of the checkbox to name = "certified_type []". After cyclically setting the value (value is set to 1, 2, 4, and so on) of all elements in certified_type to the Npower of 2, n> = 0, bitwise OR, the final result is saved to the database as an int type.

 

The result retrieved from the database can be located in the check boxes by bitwise AND operations. // For example, there are three checkboxes, value = 1, 2, and 4. If we select 1 and 4, the certified_type [] value we get is, the value obtained after bitwise OR operation in the first step is

001 1

100 4

----- |

101 5

The value of the checkbox to be stored in the database is 5, and 5 is used to perform bitwise AND operations on the value of the checkbox to be determined, if it is true, it is selected. For example, if 5 & 1 is true, the checkbox with value = 1 is selected, and 5 & 2 is false, the box with value 2 is not selected.

101 5 101

001 1 010

-----&-----&

001 1 true 000 false

Practical Use of bitwise OR and bitwise in projects

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.