使用C#,个人练习,不定时更新,如果发现错误,请告知我,感谢
2022.11.8:
static int int_shifts_are_arithmetic()
{
int a = int.MinValue;
return (a >> (sizeof(int)*8-1))==-1?1:0;
}
static int odd_ones(uint x)
{
x = x >> 16 ^ (x << 16 >> 16);
x = x >> 8 ^ (x << 24 >> 24);
x = x >> 4 ^ (x << 28 >> 28);
x = x >> 2 ^ (x << 30 >> 30);
return (x == 2||x==1)?1:0;
}
static int leftmost_one(uint x)
{
if (x == 0)
return 0;
int index = 0;
for (int w = 16; w >=1; w/=2)
{
if (x > (uint.MaxValue >> (32-w)))
{
index += w;
x = x >> w;
}
}
return (int)(x == 1 ? (x << index) : ((x + 1) << (index--)));
}
2022.11.9:
static uint rotate_left(uint x,int n)
{
return n==0?x:(x << n) + (x >> (32 - n));
}
static int threefourths(int x)
{
return ((x << 1) + x) >> 2;
}