位运算基础

 

             Binary
Value        Sample             Meaning
x 00101100 原始x的值 the original x value
~x 11010011 按位取反
-x 11010100 按位取反再加1
a|b 合并1
a&b 合并0
a^b 非进位加法,便于对某一位进行切换 x & -x 00000100 提取最末位1 extract lowest bit set x | -x 11111100 制造一块区域遮住最末位1及其左边区域 create mask for lowest-set-bit & bits to its left x ^ -x 11111000 制造一块区域遮住最末位1左边的区域 create mask bits to left of lowest bit set x & (x-1) 00101000 移除最末位1 strip off lowest bit set --> useful to process words in O(bits set) instead of O(nbits in a word) x | (x-1) 00101111 把最末尾1右边全部填为1 fill in all bits below lowest bit set x ^ (x-1) 00000111 制造一块区域遮住最末尾的1及其右部区域 create mask for lowest-set-bit & bits to its right ~x & (x-1) 00000011 制造一块区域遮住最末尾的1右边的区域 create mask for bits to right of lowest bit set x | (x+1) 00101101 最右边的0变1 toggle lowest zero bit x / (x&-x) 00001011 右移去掉所有最右边的0 shift number right so lowest set bit is at bit 0

 

 

占位符

posted @ 2015-11-27 22:10  徐王  阅读(189)  评论(0编辑  收藏  举报