c***2 发帖数: 838 | 1 Given a 32-bit integer, write a function to return the number by mirroring
all bits. |
s*****n 发帖数: 5488 | 2 don't understand the english.
【在 c***2 的大作中提到】 : Given a 32-bit integer, write a function to return the number by mirroring : all bits.
|
c***2 发帖数: 838 | 3 if you put .....101011 before a mirror, what do you see? :-)
....101011 |(mirror) = 110101......
bit 0 becomes bit 31
bit 1 becomes bit 30
and so on... |
r*********s 发帖数: 2157 | 4 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16)); |
K******g 发帖数: 1870 | 5 不就是倒过来吗?
#define REVERSE((x)) (x)=(((x)&0xFFFF0000)>>16|((x)&0x0000FFFF)<<16) \
(x)=(((x)&0xFF00FF00)>>8|((x)&0x00FF00FF)<<8) \
(x)=(((x)&0xF0F0F0F0)>>4|((x)&0x0F0F0F0F)<<4) \
(x)=(((x)&0xCCCCCCCC)>>2|((x)&0x33333333)<<2) \
(x)=(((x)&0xAAAAAAAA)>>1|((x)&0x55555555)<<1)
【在 c***2 的大作中提到】 : Given a 32-bit integer, write a function to return the number by mirroring : all bits.
|
c***2 发帖数: 838 | 6 yes, here's what I do in straightforward way:
int mirror_bits( int num)
{
int tmp=0;
int i=0;
while(num>(1<
if((1<
tmp |= 1<<(31-i);
}
i++;
}
return tmp;
} |
c***2 发帖数: 838 | 7 you are right. can you please make your codes compile and run?
【在 K******g 的大作中提到】 : 不就是倒过来吗? : #define REVERSE((x)) (x)=(((x)&0xFFFF0000)>>16|((x)&0x0000FFFF)<<16) \ : (x)=(((x)&0xFF00FF00)>>8|((x)&0x00FF00FF)<<8) \ : (x)=(((x)&0xF0F0F0F0)>>4|((x)&0x0F0F0F0F)<<4) \ : (x)=(((x)&0xCCCCCCCC)>>2|((x)&0x33333333)<<2) \ : (x)=(((x)&0xAAAAAAAA)>>1|((x)&0x55555555)<<1)
|
c***2 发帖数: 838 | 8 good one.
【在 r*********s 的大作中提到】 : x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); : x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); : x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); : x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); : return((x >> 16) | (x << 16));
|
|