您的位置:首页 > 其它

[模版] 位运算 转自USACO

2015-09-17 10:02 190 查看
a |= 0x20;		/* turn on bit 0x20 */
a &= ~0x20;		/* turn off bit 0x20 */
a ^= 0x20;		/* toggle bit 0x20 */
if (a & 0x20)  {
/* then the 0x20 bit is on */
}

Binary
Value        Sample             Meaning
x         00101100        the original x value
x & -x      00000100        extract lowest bit set
x | -x      11111100        create mask for lowest-set-bit & bits to its left
x ^ -x      11111000        create mask bits to left of lowest bit set
x & (x-1)   00101000        strip off lowest bit set
--> useful to process words in O(bits set)
instead of O(nbits in a word)
x | (x-1)   00101111        fill in all bits below lowest bit set
x ^ (x-1)   00000111        create mask for lowest-set-bit & bits to its right
~x & (x-1)  00000011        create mask for bits to right of lowest bit set
x | (x+1)   00101101        toggle lowest zero bit
x / (x&-x)  00001011        shift number right so lowest set bit is at bit 0



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: