# Several common bit operations

Source: Internet
Author: User
The following conclusions are based on the computer platform 1 void main (unsigned int x) that uses the complement code to represent a negative number)
2 {
3 // determine whether the unsigned integer x is the power of 2
4 if (x & (x-1) // if a number is a power of 2, except the highest bit is 1, the other bits are 0 (binary representation, the same below)
5 printf ("False \ n ");
6 else
7 printf ("True \ n ");
8
9 // determine whether or not an unsigned integer is in the form of 2 ^ N-1 (Same principle as above)
10 if (x & (x + 1) // if it is 2 ^ n-1, the low position is all 1
11 printf ("False \ n ");
12 else
13 printf ("True \ n ");
14
15 // The integer can be the maximum power of 2 (?) Division: the number of digits at the rightmost of the result is 1.
16 // e.g.: 100-> 4
17 printf ("% d \ n", x & (-x); // set the remaining positions to 0
18
19 // describe the position with 0 at the rightmost (Same principle as above)
20 // e.g.: 111b-> 1000b, 10-> 1
21 printf ("% d \ n ",~ X & (x + 1); // set this position to 1 and the remaining positions to 0.
22
23 // identify the mask with the suffix 0 (0 consecutive positions 1 on the right side and 0 on the other positions)
24 // e.g.: 1100b-> 0011b
25 printf ("% d \ n ",~ X & (x-1); // or
26 printf ("% d \ n ",~ (X |-x); // or
27 printf ("% d \ n", (x &-x)-1 );
28
29 // identify the mask of 1 at the rightmost and with the suffix 0 (retain the 1 at the rightmost and set all 0 positions 1 after it)
30 // e.g.: 1100b-> 0111b
31 printf ("% d \ n", x ^ (x-1 ));
32
33 // spread the rightmost 1 digit to the right
34 // e.g.: 1100b-> 1111b
35 printf ("% d \ n", x | (x-1 ));
36
37 // set the rightmost consecutive 1 position to 0
38 // e.g.: 10110b-> 10000
39 printf ("% d \ n", (x | (x-1) + 1) & x );
40}

Calculate the number of 1 bits in x: 1int Count1 (int x)
2 {
3 int n = 0;
4 while (x)
5 {
6 n ++;
7 x & = X-1;
8}
9 return n;
10}

Obtain a larger number with the same number of 1 bits. Application: 1 unsigned snoob (unsigned x) in the bitstring representing the subset of the Set)
2 {
3 unsigned smallest, ripple, ones; // e.g.: x = XXX0 1111 0000
4 smallest = x &-x; // 0000 0001 0000
5 ripple = x + smallest; // XXX1 0000 0000
6 ones = x ^ ripple; // 0001 1111 0000
7 ones = (ones> 2)/smallest; // 0000 0000 0111
8 return ripple | ones; // XXX1 0000 0111
9}

Related Keywords:

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.