Interesting bitwise operations

Source: Internet
Author: User
After reading the C Program The bitwise operation section in design language relives the cleverness and efficiency of the next bitwise operation.
Operations and operations can be used to determine the parity of an integer. Based on the binary nature, this conclusion can be easily obtained. Therefore:

 
If a & 1 A is odd; else a is even;


 
Applications with the left-shift and right-shifts should be more familiar and can perform the * 2 and/2 Operations conveniently. Previously, applications were quickly idempotent.
 
This problem is generally encountered when learning program design:

Design a program to count the binary bits with an integer of 1.

The general practice is to traverse all bits and test whether each bits is 1.

 
# Include <iostream> using namespace STD; int main () {int CNT = 0, A = 45; For (INT I = 0; I <32; I ++) {If (A & 1) CNT ++; A >>= 1 ;}// for cout <CNT <Endl ;}

One more efficient way is to find X & = (x-1 ), you can remove a binary bit with the rightmost value of 1 in X (that is, to change the rightmost bit of 1 to 0). For example:

X = 1100, so X-1 = 1011, then X & (x-1) = 1000.

Because ..... 100000. Assume that the rightmost N is 1. After this number is reduced by 1, it is changed ..... 011111 (nth digit borrowed, 1 to N-1 changed to 1)
Xxx100000
Xxx011111 &
------
Xxx000000
This prompt enables statistics and efficiency:

 
# Include <iostream> using namespace STD; int main () {int CNT = 0, A = 45; while (! = 0) {CNT ++; A & = (A-1) ;}// while cout <CNT <Endl ;}

Shift left. The combination of the right and non-operation can easily obtain the bit we need. Taking a 32-bit integer as an example, we need to take the 3-bit low-byte ~ 8 is set as a shielding bit:

~ The result of 0 is full 1:

11111111 11111111 11111111 11111111

Take the opportunity to move 6 places to the left:

11111111 11111111 11111111 11000000

Non-operation:

00000000 00000000 00000000 00111111

Take the opportunity to move two places to the left:

00000000 00000000 00000000 11111100

Similarly, the right shift can achieve this effect, and they complement each other during operations.
 

 

An exclusive or Multiple Digits in a binary integer can be easily reversed. For example:

Xxx001100xxx reversed the six digits in the binary sequence. Generally ~ Returns the inverse operator, because the Unit it uses is a short, Int, or long. Exception or can be used to solve this problem.

When two bits perform an exclusive or operation, if the two bits are the same, the result is 0. If the two bits are different, the result is 1. It is found that when a bit is different from 1, the result is the non-bitwise of this bit. When a bit is different from 0, the result remains unchanged. So:

Xxx001100xxx

000111111000 ^ (for the calculation of the shielding bit, refer to the above)

-------
Xxx110011xxx

 

An interesting conclusion is that an integer is consecutive or the same number, and the result is still the integer, which can be simply encrypted. For example:

101

110 ^ (encrypted)

---

011

110 ^ (decryption)

---

101
To fool girls:

00000000 00000000 00000010 00001000 (520)

00000000 00000000 00000101 00100010 (1314)
 

Mastering the application of bit operations also shows that you have a certain understanding of computer data storage, and you will have some experience during the interview. Matrix67 is recommended hereArticle.

After this article is completed

troublic http://www.daoluan.net/

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.