1

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.

11
  • 4
    First think about what the function is actually doing - it's testing all the odd-numbered bits to see if they are all 1. So how would you do that in a single operation using bitwise operators ? (Think masks.) Commented Jul 18, 2016 at 10:00
  • 3
    How is this a duplicate of the linked question? Commented Jul 18, 2016 at 10:08
  • 1
    @Silverfin: your logic is a bit off - you probably want to use a mask of all odd bits (0xaaaaaaaa), since these are the bits you are interested in. Then the function would just be return (n & mask) == mask; (you don't need an if). If you can't use == then think about how to implement that with further bitwise operations. Commented Jul 18, 2016 at 10:12
  • 1
    Yes, that looks promising, assuming you are allowed to use the logical NOT operator (!). Commented Jul 18, 2016 at 10:28
  • 1
    @PaulR I can, i've got a better grasp of things. thanks you. Commented Jul 18, 2016 at 10:40

2 Answers 2

6

Maybe this can help:

int loop(int x) {
    x = x & 0xaaaaaaaa; // Set all even numbered bits in x to zero
    x = x ^ 0xaaaaaaaa; // If all odd numbered bits in x are 1, x becomes zero
    x = !x;             // The operation returns 1 if x is zero - otherwise 0
    return x;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe i dont get it, but this does return a value greater then 1, doesnt it?
Oh i see, because ! is logical not. Clever!
0

Your code tests all odd bits and returns 1 if all of them are set. You can use this bitmask: ...0101 0101 0101 Which, for 32 bits is 0xAAAAAAAA. Then you take your value und bitwise-and it. If the result is the same as your mask, it means all bits are set.

int testOddBits(int x) {
    return (x & 0xAAAAAAAA) == 0xAAAAAAAA;
}

11 Comments

@PaulR no it isnt, because the return is int, not bool.
oh i see, i didnt get you. i though you would suggest return x & 0xAAAAAAAA. You are right, sry.
@Silverfin Your function does not return multiple times. You can only return once. The function will be aborted, and the single value will be returned.
Funny to see how each_and_every answer in [C] is downvoted even tho it's a correct answer to the question, and may (nor may not) have a small slip. And funny how all these "experts" keep their mouth shut instead of answering, and then nitpick those who do.
@RaulR as far as i've understood, the problem is the loop, and ?: is explained in the link provided by the OP. so I cannot see why Mario's answer is "not helpful", it hints to using a mask to AND with it, rather than using a loop. Let the OP decide if the return type matters or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.