About Negative bit computing

An original title on to prove safety OFFER, drilled a dead end

Seeking the number of binary 1

Problems in the negative, because you want to keep the nature of the negative, so negative right shift operation will be re-fill 1 in the first place, right after several changes to 0xFFFFFFFF.

If you want to remove the sign bit negative, consider using:

int a=-127;

a=a^(0x80000000);

0x80000000 is in the first 1 to 32, or to vary after a 31-bit data, the result not 0 XOR 0, while for the first, and 1 XOR 0 will be zero.

On this basis the right is obtained in addition to a number other than a number of the first one.

Finally, a number of the negative first place to fill.

In fact, when treated by a left shift can be obtained in the form of a number 1, and so totally unnecessary trouble.

In addition there prove safety offer a solution:

(n-1)&n;

Suppose that for n, which is not a right of the first position m is 0, then n-1 so that m is 0, 1 are all right m. All set to 0 and n-1 n m will be the right and m & operation.

therefore

while(n)

  (n-1)&n;

N 1 would sequentially eliminate its right

Guess you like

Origin www.cnblogs.com/lxy-xf/p/11343688.html