I'm trying to recreate this function:
int test(int x) {
int i;
for (i = 0; i < 32; i+=2)
if ((x & (1<<i)) == 0)
return 0;
return 1;
}
But only using these bit-wise operators: !, ~, &, ^, |, +, <<, and >>
(Meaning no loops or if statements either)
I am so confused with this question I have been staring at it for like a hour and am still not sure where to start.
I understand that basically it is taking x comparing it with 2^i where i is 0-31 and then returning 0 if x and 2^i do not share any of the same bits and returning 1 otherwise.
But I feel like there is a more simple, non-bit focused explanation that summarizes this better and if someone could even just give me that it would be a huge help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…