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
If so, are there any problems that could arise from this?
If not, why not?? What alternatives do I have?
My function:
int any_odd_one(unsigned x)
{
int mask = 10101010101010101010101010101010
if(x & mask)
{
return 1;
}
else
{
return 0;
}
}
Thanks in advance for any assistance!
-Matt
You can’t use binary literals in C. Instead, use hexadecimal or octal notation.
In your case, you’d use
unsigned mask = 0xaaaaaaaasince10101010...is0xaaaaaaaawhen expressed in hexadecimal (each1010isain hex).