# Some Useful Bit Manipulations

Translator: Funnyyanne

Author: labuladong

This article is divided into two parts. The first part lists a few interesting bitwise operations, second part explains n&(n-1) trick commonly used in algorithm. By the way, I’m going to show you the algorithm for this trick. Because Bit manipulation is simple, it is assumed that the reader already knows the three basic operations of AND, OR, XOR.

Bit Manipulation can play a lot of fucking trick, but most of these tricks are too obscure, there is no need to dig in. We just need to remember some useful operations.

## Ⅰ.Interesting Bit manipulations

Use OR '|' and space bar coverts English characters to lowercase

Use AND '&' and underline coverts English to uppercase.

Use XOR '^' and space bar for English characters case exchange.

PS：The reason why the operation can produce strange effects is ASCII encoding. Characters are actually Numbers, it happens that the Numbers corresponding to these characters can get the correct result through bit manipulations, if you interested in it, you can check ASCII table, this article does not expand it.

Determine if the sign of two numbers are different

PS：This technique is very practical, and uses sign bit complement encoding. If you don't use the bit operation to determine whether the sign is different, you need to use if else branch, which is quite troublesome. Readers may want to try to use products or quotients to determine whether two numbers have different signs, but this processing method may cause overflow and cause errors. (For complement coding and overflow, see the previous article.)

Swap two Numbers

Plus one

Minus one

PS：These three operations just Show off, No practical use, we just know it.。

## Ⅱ.Algorithm common operations n&(n-1)

This operation is the common algorithm, the function is eliminated the number n of the binary representation of the last 1.

It is easy to understand by looking at the picture：

Count Hamming Weight（Hamming Weight）

Be to let you return several ones in the binary representation of n's one. Because n & (n-1) can eliminate the last one, you can use a loop to eliminate 1 and count at the same time until n becomes 0.

Determine if a number is an exponent of 2

If a number is an exponent of 2, its binary representation must contain only one 1:

If you use the bit operation technique, it is very simple (note the precedence of the operator, the parentheses cannot be omitted) :

The above are some interesting / common bit manipulation. In fact, there are many bit manipulation techniques. There is a foreign website called Bit Twiddling Hacks which collects almost all black technology gameplays of bit manipulation. Interested readers can search to view.

Last updated