什么时候用bitwise operators

来源:百度知道 编辑:UC知道 时间:2024/07/04 11:25:02
|
&
~
这些

按位运算

按位运算能干些什么?

给大家设计一些能打开思路的练习题

去掉最后一位(101101->10110) x shr 1
在最后加一个0(101101->1011010) x shl 1
在最后加一个1(101101->1011011) x shl 1+1
把最后一位变成1(101100->101101) x or 1
把最后一位变成0(101101->101100) x or 1-1
最后一位取反(101101->101100) x xor 1
右数第k位取反(101101->101001,k=3) x xor (1 shl (k-1))
取末三位(1101101->101) x and 7
取末k位(1101101->1101,k=5) x and (1 shl k-1)
把末k位变成1(101001->101111,k=4) x or (1 shl k-1)
末k位取反(101001->100110,k=4) x xor (1 shl k-1)

把右边连续的1变成0(100101111->100100000) x and (x+1)
把右起第一个0变成1(100101111->100111111) x or (x+1)
把右边连续的0变成1(11011000->11011111) x or (x-1)
取右边连续的1(100101111->1111) x xor (x+1) shr 1
去掉右起第一个1的左边(100101000->1000,树状数组) x and (x xor (x-1))

看一看用位运算来做的n皇后问题的神速程序超短代码:

CODE:
procedure test(row,ld,rd:longint); //注释以八皇后为例
var
pos,p:longint;
begin
if row<>