Several commonly used bitwise operator techniques

Source: Internet
Author: User
Control hardware often involves opening/closing specific bits or viewing their status. The bitwise operator (<<,>>,~,&,|,^) provides this capability. In the following example, Lottabits represents a value, bit represents the value of a special location. Bits are numbered from right to left, starting with 0, so that the nth bit has a value of 2 n (usually only one is 1, which is for brevity). For example, only an integer with a 3rd bit of 1 is the value of 2 for the 3-time square. ("C + + Primer Plus" Appendix E)
1. Open the bit
Opens a bit that will open the bit in the lottabits corresponding to the bit representation
Lottabits=lottabits|bit;
2. Toggle Bit
Toggle bit, will swap lottabits with bit
Lottabits=lottabits^bit;
3. Close the bit
Turns off the bit that corresponds to the bit representation in the Lottabits
lottabits=lottabits&~bit;
4. Test the value of the bit
The value of the test bit
if (lottabits&bit);

Several commonly used bitwise operator techniques

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.