## (译)你必须知道的位运算技巧 Low Level Bit Hacks You Absolutely Must Know

from HackerMonthly-issue15
By Peteris Krumins

& -  and
| -  or
^ -  xor
~ -  not
<< -  左移
>> -  右移

if ((x & 1) == 0) {
x is even
}
else {
x is odd
}

00101011
& 00000001
--------
00000001

01100010
& 00000001
--------
00000000

AND的结果是0,意味着98的b0是0,指定的整数为偶数.

-98二进制表示为 10011110. 同样b0 为0,AND后结果为0

if (x & (1<<n)) {
n-th bit is set
}
else {
n-th bit is not set
}

1 00000001 (等同于 1<<0)
1<<1 00000010
1<<2 00000100
1<<3 00001000
1<<4 00010000
1<<5 00100000
1<<6 01000000
1<<7 10000000

122的第三位是否有值,下面这个操作会找到答案:

122 & (1<<3)

122的二进制表示01111010,1<<3 为 00001000.

01111010
& 00001000
--------
00001000

11011111 (-33 in binary)
& 00100000 (1<<5)
--------
00000000

y = x | (1<<n)

01111000 (120 in binary)
| 00000100 (1<<2)
--------
01111100

10001000 (-120 in binary)
| 01000000 (1<<6)
--------
11001000

y = x & ~(1<<n)

~1 11111110 (same as ~(1<<0))
~(1<<1) 11111101
~(1<<2) 11111011
~(1<<3) 11110111
~(1<<4) 11101111
~(1<<5) 11011111
~(1<<6) 10111111
~(1<<7) 01111111

01111111 (127 in binary)
& 11101111 (~(1<<4))
--------
01101111

y = x ^ (1<<n)

01110101
^ 00100000
--------
01010101

01010101
^ 00100000
--------
01110101

y = x & (x-1)

01010111 (x)
& 01010110 (x-1)
--------
01010110

01011000 (x)
& 01010111 (x-1)
--------
01010000

10000000 (x = -128)
& 01111111 (x-1 = 127 (溢出))
--------
00000000

11111111 (x = 都是1 1)
& 11111110 (x-1)
--------
11111110

00000000 (x 没有是1的位)
& 11111111 (x-1)
--------
00000000

1. 位列中有1. 减1会将低位变为1并将最后一个1的位变0.这步再和原来的值AND屏蔽了从最后一个1的后面部分.

2.位列中没有1都是0.这是减去1将溢出所有位都变成1. 和都是0的位列AND 还是0.

y = x & (-x)

10111100 (x)
& 01000100 (-x)
--------
00000100

01110000 (x)
& 10010000 (-x)
--------
00010000

00000001 (x)
& 11111111 (-x)
--------
00000001

10000000 (x = -128)
& 10000000 (-x = -128)
--------
10000000

11111111 (x = all bits one)
& 00000001 (-x)
--------
00000001

00000000
& 00000000 (-x)
--------
00000000

y = x | (x-1)

10111100 (x)
| 10111011 (x-1)
--------
10111111

01110111 (x)
| 01110110 (x-1)
--------
01110111

00000001 (x)
| 00000000 (x-1)
--------
00000001

10000000 (x = -128)
| 01111111 (x-1 = 127)
--------
11111111

11111111 (x = -1)
| 11111110 (x-1 = -2)
--------
11111111

00000000 (x)
| 11111111 (x-1)
--------
11111111

--------
10001000 (~x)
& 01111000 (x+1)
--------
00001000
00000001 (x)
--------
11111110 (~x)
& 00000010 (x+1)
--------
00000010
10000000 (x = -128)
--------
01111111 (~x)
& 10000001 (x+1)
--------
00000001
11111111 (x = no rightmost 0-bit)
--------
00000000 (~x)
& 00000000 (x+1)
--------
00000000

00000000 (x)
--------
11111111 (~x)
& 00000001 (x+1)
--------
00000001

y = x | (x+1)

10111100     (x)
| 10111101   (x+1)
--------
10111101

01110111     (x)
| 01111000   (x+1)
--------
01111111

00000001     (x)
| 00000010   (x+1)
--------
00000011

10000000      (x = -128)
| 10000001    (x+1)

--------
10000001

11111111 (x = no rightmost 0-bit)
| 00000000 (x+1)
--------
11111111

00000000 (x)
| 00000001 (x+1)
--------
00000001

Python 代码:

def int_to_bin(num, bits=8):

  r = ''

  while bits:

    r = ('1' if num&1 else '0') + r

    bits = bits - 1

    num = num >> 1

print r

C代码:

void int_to_bin(int num) {

  char str[9] = {0};

  int i;

  for (i=7; i>=0; i--) {

    str[i] = (num&1)?'1':'0';

    num >>= 1;

  }

  printf("%s\n", str);

}

posted on 2012-08-31 11:45  Haozes  阅读(1530)  评论(0编辑  收藏  举报