Bit operation summary

Source: Internet
Author: User

1. Only integer operations can be performed, such as char, short, int, and long (whether signed or unsigned)

 

2. >>,< the priority is smaller than the arithmetic operator, so the parentheses in the expression a = B + (c> 1) cannot be less. Note that the right shift> operator,For the unsigned number, the left side is filled with 0, while for the signed number, it may be filled with 0 or "symbol bit". Therefore, when we always want to fill the number with 0, it must be declared with unsigned (left shift <always fills with 0, whether it is a signed number or an unsigned number)

 

3. Several examples of The C Programming language are classic:

 /* Obtain the n bits starting from position P */
Unsigned int getbits (unsigned x, int p, int n)
{
Return (x> (p + 1-n ))&(~ (~ 0 <n ));
}

/* Reverse the n bits starting from position p, And the rest remain unchanged */
Unsigned int invert (unsigned x, int p, int n)
{
Return (x ^ (~ (~ 0 <n) <(p + 1-n )));
}
11011001
^ 00111100 (just read this)

 

4. In the beauty of programming, the bitwise operation of Question 1 in Chinese chess is also very concise and beautiful, and it is recorded here

 # Define HALF_BITS_LENGTH 4
# Define FULLMASK 255

# Define LMASK (FULLMASK <HALF_BITS_LENGTH) // 11110000
# Define RMASK (FULLMASK> HALF_BITS_LENGTH) // 00001111

# Define RSET (B, n) (B = (B & LMASK) | n) // reset n to the right
# Define LSET (B, n) (B = (B & RMASK) | (n <HALF_BITS_LENGTH) // reset n on the left

# Define RGET (B) (B & RMASK) // obtain the right value
# Define LGET (B) (B & LMASK)> HALF_BITS_LENGTH) // obtain the left Value

It is easy to modify the above slightly to fit any bit.

 

5. The following content is reproduced from: http://blog.sina.com.cn/s/blog_66ad7bba0100hf9k.html

 Q: What should I do if I want to remove the first occurrence 1 in a binary number, such as 6-> 4, 14-> 12, 5-> 4?
Answer: The fastest algorithm: n & (n-1)

The problem is, what should I do if I want to obtain the first occurrence 1 in a binary digit, such as 6-> 2, 7-> 1, 12-> 4?
Answer: Likewise, you can get n-(n & (n-1 ))

Problem: judge whether a number is a power of 2.
Answer: (n & (n-1) = 0 & n> 0

Problem: Write the sign function. The parameter is int n. If n is negative, return-1. If positive, return 1. 0. Return 0.
Solution 1: return !! N-(unsigned) n> 31) <1)
Solution 2: return !! N + (n> 31) * 2 (this is not necessarily true for all compilers)

Problem: to calculate the average of two integers, consider overflow.
A: This calculation method ensures that it is not affected by the overflow problem: (x & y) + (x ^ y)> 1)

Problem: Calculate a power greater than n and the smallest power of 2, such as 3-> 4, 6-> 8,100-> 128,256-> 512
Answer:
Int calc (int n)
{
N | = n> 1;
N | = n> 2;
N | = n> 4;
N | = n> 8;
N | = n> 16;
Return n + 1;
}

 

 

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.