cssap_2_homework_bit_operation

来源:互联网 发布:linux上安装maven 编辑:程序博客网 时间:2024/06/06 08:45
function bitParity(int x) {   x ^= x >> 16;   x ^= x >> 8;   x ^= x >> 4;   x &= 0xf;   return (0x6996 >> x) & 1;}

Finding odd number of bits in an integer

0x6996 serves as a lookup table - for each number between 0 and 15 you choose one of its bits


nt

 

leftmost_one

(unsigned x){ 

    x |= (x >> 

1

); 

    x |= (x >> 

2

); 

    x |= (x >> 

4

); 

    x |= (x >> 

8

); 

    x |= (x >> 

16

); 

    

return

 x^(x>>

1

); 

nt

 

leftmost_one

(unsigned x){ 

    x |= (x >> 

1

); 

    x |= (x >> 

2

); 

    x |= (x >> 

4

); 

    x |= (x >> 

8

); 

    x |= (x >> 

16

); 

    

return

 x^(x>>

1

); 

int leftmost_one(unsigned x){     

x |= (x >> 1);     

x |= (x >> 2);     

x |= (x >> 4);     

x |= (x >> 8);    

x |= (x >> 16);     

return x^(x>>1); 

}

Generate mask indicating leftmost 1 in x

int lower_one_mask(int n){    

 return (2<<(n-1)) - 1;

}

or

{

return ~(~0 << n);

}

Mask with least signficant n bits set to 1


0 0
原创粉丝点击