Conditional use of bitwise operators

How is a conditional statement represented using bitwise operators?

Edit: Sorry for the poor explanation. This is a homework question when I have to implement a conditional statement using only bitwise operations. It would be simple if the statements were allowed, but they should be strictly bitwise operators. A function takes three functions and works just like a regular conditional statement. This first int is computed, 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 of great help. Thanks!

+4
source share
5 answers

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

Your editing is not entirely clear, but I assume you need to implement the equivalent

a ? b : c 

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

 a != 0 ? b : c 

One way to achieve this is to find a way to turn the non-zero value of a into a pattern with bits of the same type using only bitwise operators. If we figure out how to do this, the rest will be easy. Now I don’t immediately remember some ingenious tricks that will do this (they exist, I think), and I don’t know exactly which operators are allowed and which are not, so at the moment I'm just using 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) at least one bit was set in the original a , then the above should result in all bits of a being set to 1. (Assume that we work with unsigned integers to avoid problems associated with the transfer of signed values). Again, there must be a smarter way to do this, I'm sure. For example: a = !a - 1 , but I don’t know if ! and - .

Once we have done this, the original conditional statement becomes equivalent

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

Done.

+10
source

This is not so, in principle. A conditional statement will evaluate only one of the second or third operands; bitwise operators always evaluate both operands.

I don’t think it actually makes sense to think of a conditional operator in terms of bitwise operators, to start with ... for example, if the second and third operands are pointer types, you would not want to think about from the point of view of bitwise operations, not so is it? Address to the conditional operator separately bitwise operators - you will not make any benefits, trying to unite them.

+8
source

I think the OP is looking for a way to express things that usually require a conditional reckless way. For example (assuming unsigned x,y,z; and x bounded by INT_MAX ):

 if (x>2) y+=z; 

may be expressed as:

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

This example comes to mind because I used it in “ramified binary sorting” in a number of cases. When the size of the desired array is constant, this allows you to fully expand the search cycle in a sequence of operations without branches and compile only a few operation codes for each step. Those who object to writing "assembler in C" may prefer to write it:

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

and hope the compiler generates an equivalent bitmask or cmov instruction. cmov

+1
source

At the most basic level, it becomes electronics. See this . I can not think of any other application for your question.

0
source

If you refer to a triple-choice operator, this can be represented using bitwise operations, but only in some cases (in fact, this is an optimization for using bitwise operations in some cases). For example: (getsomevalue() == 1) ? somepointer : NULL; (getsomevalue() == 1) ? somepointer : NULL; can be represented as somepointer & ~((unsigned)(getsomevalue()) - 1); if getsomevalue() returns only 1 or 0 (aka BOOL)

0
source

All Articles