I'm trying to replicate the function of a loop using only bitwise and certain operators including ! ~ & ^ | + << >>
int loop(int x) {
for (int i = 1; i < 32; i += 2)
if ((x & (1 << i)) == 0)
return 0;
return 1;
}
Im unsure however how to replicate the accumulating nature of a loop using just these operators. I understand shifting << >> will allow me to multiply and divide. However manipulation using ! ~ & ^ ~ has proven more difficult. Any Tips?
http://www.tutorialspoint.com/cprogramming/c_operators.htm
Edit: I understand how the addition of bits can be achieved, however not how such an output can be achieved without first calling a while or for loop.
0xaaaaaaaa), since these are the bits you are interested in. Then the function would just bereturn (n & mask) == mask;(you don't need anif). If you can't use==then think about how to implement that with further bitwise operations.!).