Advanced Bit manipulation Tips

Source: Internet
Author: User

"Here we are going to discuss some advanced bit manipulation techniques that will greatly increase the speed of your program if used properly"

We have discussed some simple bit manipulation methods, and if you are not familiar with bit operations you can first browse through the "simple bit manipulation techniques you must know", here we go:

Tip 6: Set the bit bit with the rightmost value to 1 to 0

y = x & (x1)

Finally started a little meaning, skill 1 to tip 5 to tell the truth is a little pediatrics.

This statement will look right-to-left with a bit position of 1 with a value of 0. For example: integer 00101010 After this operation becomes 00101000, integer10000 is manipulated into 0 because there is only one bit value of 1.

For more examples:

`01010111(x)&01010110(X-1)    --------01010110     01011000(x)&01010111(X-1)    --------01010000     10000000(x =- -)&01111111(X-1=127(with overflow))--------00000000     11111111(x = ALL bits1)&11111110(X-1)    --------11111110     00000000(x = no rightmost1-bits)&11111111(X-1)    --------00000000

Looking at these examples, you will find that there are two types of situations:

1, this value has a value of 1 bits, minus one will be the value of a bit bit to the right of the low bit is all set to 1, itself becomes 0, and the original number and operation, the result is this position is 0.

2, this value is 0, then there is no value of 1 bit, minus one caused by overflow, all bits become 1 (signed integer), all 0 and all 1 do and the result of the operation is 0.

Tip 7: Isolate bits with a rightmost value of 1

y = × & (-X)

That is to find a bit with the rightmost value of a number of 1, the other position is 0. The above statement implements this function. For example, 01010100 (Blackbody is the rightmost value 1 bit) and the result is 00000100.

For more examples:

`10111100(x)&01000100(-x)--------00000100     01110000(x)&10010000(-x)--------00010000     00000001(x)&11111111(-x)--------00000001     10000000(x =- -)&10000000(-X =- -)    --------10000000     11111111(x =All bits one)&00000001(-x)--------00000001     00000000(x = ALL bits0, no rightmost1-bit)&00000000(-x)--------00000000

This operation is valid in the range of complement notation, and in the binary complement system,-X is represented as-x+1.

Let's look at two different things:

1, there is a bit of the rightmost value of 1, we use this bit center (temporarily labeled BI), the other bits are divided into the left and right side, all the bits on the left are 0 (bi-1,...,b0), the bit on the far side do not know what (bn,...,bi+1).

OK, now for-X, first, the bi conjugation 0, then the bi-1,...,b0 conjugation 1, then flip bn,...,bi+1, and finally the result +1. We get the complementary form of-X.

Before +1, since the bi-1,...,b0 bit is 1, plus one is 0 until the BI bit is encountered.

Overall, calculate-X is the flip bn,...,bi+1 bit, the BI bit is unchanged, and the bi-1,...,b0 becomes 0.

Now it is clear that X & is bn,...,bi+1 to the 0,BI bit and bi-1,...,b0 to 0.

2, there is no bit value of 1, the value of 0,0 's twos complement or 0, and the result of the operation is 0.

We have strictly elaborated this technique to be correct.

Tip 8: Right-spread the rightmost bit with a value of 1

y = x | (x1)

Take a look at the example to see what is going on, the number 01010000 is calculated to find the rightmost value is 1 bit, and then the bit to the right of all the bit position is 1.

This technique has a flaw, that is, if the x=0 results are all 1.

Take a look at more examples:

`10111100(x)|10111011(X-1)    --------10111111     01110111(x)|01110110(X-1)    --------01110111     00000001(x)|00000000(X-1)    --------00000001     10000000(x =- -)|01111111(X-1=127)    --------11111111     11111111(x =-1)|11111110(X-1= -2)    --------11111111     00000000(x)|11111111(X-1)    --------11111111

Although not as strict as the previous technique, let's simply prove it (it's not an academic paper that's a waste of time.) Two things, from a simple start:

1, there is no rightmost value of 1 bit, this case x=0,x-1=-1,-1 of the binary complement is expressed as 11111111 (as if forgetting to say, we discuss the number of the 8bit range, also without losing generality), 0 with 11111111 or the result of the operation is 11111111 (not the desired result, but the fact is very brutal).

2, there is a bit of the rightmost value of 1, we will be re-applied, or divided into two parts (as in the previous example), the calculation x-1 only affect the right bit, the bi position is 0, all the right side is 1. Now the result x-1 and small do or operations, the left part of the bit is unchanged, bi bit or 1, the right side has become 1. The result is that the rightmost bit with a value of 1 spreads to the right.

Tip 9: Isolate bits with a rightmost value of 0

y = ~x & (x+1)

In contrast to tip 7, find the bit with the rightmost value of x 0, place the other position at 0, and this position is 1. For example x=10101011 (find 0 in bold), the result of the operation is 00000100.

See below for more examples:

`10111100(x)--------01000011(~x)&10111101(x+1)    --------00000001     01110111(x)--------10001000(~x)&01111000(x+1)    --------00001000     00000001(x)--------11111110(~x)&00000010(x+1)    --------00000010     10000000(x =- -)    --------01111111(~x)&10000001(x+1)    --------00000001     11111111(x = no rightmost0-bit)--------00000000(~x)&00000000(x+1)    --------00000000     00000000(x)--------11111111(~x)&00000001(x+1)    --------00000001

Simple proof: Assuming that there is a bit of the rightmost value of 0, then-X and x+1 will this bit of 1,-x and x+1 with the left part of the bit position is 0 (-X to the left part of X.), the right part of the result is also 0 (x+1 the right part of the x 0), so only the BI bit is 1.

Tip 10: Reverse the bit with the rightmost value 0
y = x | (x+1)
For example, the result is 10100111 after the x=10100011 operation. For more examples:
`10111100(x)|10111101(x+1)    --------10111101     01110111(x)|01111000(x+1)    --------01111111     00000001(x)|00000010(x+1)    --------00000011     10000000(x =- -)|10000001(x+1)    --------10000001     11111111(x = no rightmost0-bit)|00000000(x+1)    --------11111111     00000000(x)|00000001(x+1)    --------00000001

In fact, X and x+1 do or do not lose any information, the X plus 1 is filled with the rightmost 0, the result is max{x,x+1}. When the x+1 overflows, the result is that 0,X does not have a bit value of 0, and if no overflow result is x+1, or the rightmost 0 bit is set to 1 after the operation.

Advanced Bit manipulation Tips

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.