copyMSB - set all bits of result to most significant bit of x.
Examples: copyMSB(0x80000000) = 0xFFFFFFFF copyMSB(0x70000000) = 0x00000000 Legal ops: ! ~ & ^ | + << >> Max ops: 5
不能用if-else。
unsigned int copyMSB(unsigned int x)
{
return x >> 31 << 31;
}
int main()
{
printf("%d\n", copyMSB(0x80000000));
printf("%d\n", copyMSB(0x70000000));
}
#include <stdio.h>
unsigned int copyMSB(unsigned int x)
{
return ~( 0xFFFFFFFF+ (x>>31) );
}
int main()
{
printf("%08X\n", copyMSB(0x80000000));
printf("%08X\n", copyMSB(0x70000000));
}
有没有规定数据位数…………
这样也行:
#include <stdio.h>
unsigned int copyMSB(int x)
{
return x>>31 ;
}
int main()
{
printf("%08X\n", copyMSB(0x80000000));
printf("%08X\n", copyMSB(0x70000000));
}