位元運算,數值比較 換算

栗子
沒力女子
Published in
Jun 22, 2021

C/C++

位元運算子: << SHIFT LEFT 、 >> SHIFT RIGHT 、 & AND 、 | OR 、 ^ XOR 、 ~ NOT

int is_odd(int x)  
{
return x & 1; // x % 2; // 若回傳1則為奇數,回傳0則為偶數。
}
bool is_power_of_2(int x)
{
return (x & -x) == x;
}
float_to_binary(float *x)
{
int out =*((int*)&*x);
for (int c = 31; c >= 0; c--)
{
int k = out >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
}
int size=10000;
int main()
{
float x;
while((scanf("%f",&x))!=EOF)
{
float_to_binary(&x);}return 0;
}

http://puremonkey2010.blogspot.com/2011/05/c-bitwise-operation.html

--

--