32位整数掩码 [英] 32 Bit Integer Mask
问题描述
我整理了一些功课CSE和我有更大的声明位大小的整数一个快速的问题。我的任务是实现如果x的任何奇数位是1,返回1的函数(x的假设尺寸是32位),否则返回0。
I'm finishing up some CSE homework and I have a quick question about declaring integers of larger bit sizes. My task is to implement a function that returns 1 if any odd bit of x is 1 (assuming size of x is 32 bits) and returns 0 otherwise.
我可以在申报的位值的整数:
Am I allowed to declare an integer with the bit value:
10101010101010101010101010101010
10101010101010101010101010101010
如果是这样,有可能由此产生的任何问题?
如果不是,为什么不?我有什么选择呢?
If so, are there any problems that could arise from this? If not, why not?? What alternatives do I have?
我的功能:
int any_odd_one(unsigned x)
{
int mask = 10101010101010101010101010101010
if(x & mask)
{
return 1;
}
else
{
return 0;
}
}
提前感谢任何帮助!
Thanks in advance for any assistance!
-Matt
推荐答案
您不能在C.使用二进制字面相反,使用十六进制或八进制符号。
You can't use binary literals in C. Instead, use hexadecimal or octal notation.
在你的情况,你会使用无符号的面具= 0xaaaaaaaa
,因为 10101010 ...
是 0xaaaaaaaa
时前$ p $十六进制pssed(每个 1010
是 A
十六进制)。
In your case, you'd use unsigned mask = 0xaaaaaaaa
since 10101010...
is 0xaaaaaaaa
when expressed in hexadecimal (each 1010
is a
in hex).
这篇关于32位整数掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!