位运算很强大很神奇
下面是两段代码
求两数的平均数
1 function ave(a,b:longint):longint;
2 begin
3 exit((a and b)+(a xor b)>>1);
4 end;
求一个十进制数转换成二进制后1的个数
function num(n:longint):longint;
begin
n:=(n and $55555555)+((n>>1) and $55555555);
n:=(n and $33333333)+((n>>2) and $33333333);
n:=(n and $0F0F0F0F)+((n>>4) and $0F0F0F0F);
n:=(n and $00FF00FF)+((n>>8) and $00FF00FF);
n:=(n and $0000FFFF)+((n>>16) and $0000FFFF);
exit(n);
end;