tags:

views:

151

answers:

5

How is the conditional operator represented using bitwise operators?

Edit: Sorry for the poor explanation. It is a homework question where I have to implement the conditional operator using only bitwise operations. It would be simple if if statements were allowed, however it has to be strictly bitwise operators. The function takes in three ints and works just like the normal conditional operator. This first int is evaluated and one of the last two is returned depending on the value of the first. I was hoping there would be a simple algorithm for this. Any ideas on where to start would be a great help. Thanks!

+6  A: 

It's not, basically. The conditional operator will only evaluate one of the second or third operands; bitwise operators always evaluate both operands.

I don't think it really makes sense to think of the conditional operator in terms of bitwise operators to start with... for example, if the second and third operands are pointer types, you wouldn't want to think of those in terms of bitwise ops, would you? Treat the conditional operator separately to the bitwise operators - you won't do yourself any favours by trying to amalgamate them.

Jon Skeet
Have to agree with the second paragraph here. Better to think of them both in terms of what they do at the machine level than to try to relate one to the other.
T.E.D.
A: 

Are shifts allowed as bitwise operators? Are arithmetic operators allowed?

Your edit is not entirely clear, but I assume that you need to implement an equivalent of

a ? b : c

where a, b and c are integers. This is in turn equivalent to

a != 0 ? b : c

One way to achieve that is to find a way to turn non-zero value of a into an all-ones bit pattern using only bitwise operators. If we figure out how to do that, the rest would be easy. Now, I don't immediately remember any ingenious tricks that would do that (they do exist I believe), and I don't know for sure which operators are allowed and which are not, so for now I will just use something like

a |= a >> 1; a |= a >> 2; a |= a >> 4; a |= a >> 8; a |= a >> 16;
a |= a << 1; a |= a << 2; a |= a << 4; a |= a << 8; a |= a << 16;

For a 32-bit integer type, if (and only if) there was at least one bit set in the original a, the above should result in all bits of a set to 1. (Let's assume we are working with unsigned integers, to avoid the issues associated with shifting of signed values). Again, there must be a more clever way to do that, I'm sure.

(For example: a = !a - 1 but I don't know if ! and - are allowed.)

Once we've done that, the original conditional operator becomes equivalent to

(a & b) | (~a & c)

Done.

AndreyT
To make a an all ones bit pattern, could I just use: a = !a; a = ~a. What if a was zero?
Matt
@Matt: I don't understand. What is `a = !a; a = ~a`? Firstly, `!` is not a bitwise operator. Can you use it? Secondly, the whole point is to turn non-zero `a` in all-ones bit pattern and leave zero `a` unchanged (i.e. all-zero bit pattern).
AndreyT
I didn't realize that would change a non-zero a into an all ones bit pattern while leaving a zero a alone. This works perfectly. Thank you very much!
Matt
A: 

At the very basic level it becomes electronics. See this. I cannot think of any other application for your question.

slashmais
A: 

If your refering to the ternary selection operator, this can be represented using bitwise operations, but only in certain cases(in fact its an optimization to use bitwise ops in some cases). Eg: (getsomevalue() == 1) ? somepointer : NULL; can be represented as somepointer & ~((unsigned)(getsomevalue()) - 1); assuming getsomevalue() only returns 1 or 0(aka BOOL)

Necrolis
A: 

I think the OP is looking for a way to express things which would normally require a conditional in a branchless way. For instance (assuming unsigned x,y,z; and x is bounded by INT_MAX):

if (x>2) y+=z;

can be expressed as:

y += z & -(2-x >> sizeof(unsigned)*CHAR_BIT-1);

This example comes to mind because I've used it in a "branchless binary sort" on a number of occasions. When the size of the array being searched is constant, this allows completely unrolling the search loop into a sequence of operations with no branches, and compiles to just a few opcodes per step. Those who object to writing "assembler in C" might prefer it to be written:

y += (x>2) ? z : 0;

and hope the compiler generates an equivalent bit mask or cmov instruction. :-)

R..