简体   繁体   English

条件使用按位运算符

[英]Conditional Using Bitwise Operators

How is the conditional operator represented using bitwise operators? 如何使用按位运算符表示条件运算符?

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. if允许语句,那将很简单,但它必须是严格按位的运算符。

Only the operators ! 只有经营者! , ~ , & , ^ , | ~&^| , + , >> , and << can be used. +>><<可以使用。 No if statements or loops can be used. 没有if可以使用的语句或循环。

The function takes three ints and works just like the normal conditional operator. 该函数有三个整数,就像普通的条件运算符一样工作。 The first argument is evaluated as either zero or non-zero. 第一个参数被评估为零或非零。 If the first argument is zero then the second argument is returned. 如果第一个参数为零,则返回第二个参数。 If the first argument is non-zero then the third argument is returned. 如果第一个参数不为零,则返回第三个参数。

I was hoping there would be a simple algorithm for this. 我希望有一个简单的算法。 Any ideas on where to start would be a great help. 任何关于从哪里开始的想法都将是一个很大的帮助。

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. 其中abc是整数。 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. 实现这一目标的一种方法是找到一种方法,仅使用按位运算符将a的非零值转换为全1位模式。 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). 对于32位整数类型,如果(且仅当)有一个在原来的至少一个位设置a ,上面应该导致所有位a设置为1(假设我们正在与无符号整数工作,以避免与签名值转移相关的问题)。 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 ! 例如: a = !a - 1 ,但我不知道是否! and - are allowed. 并且-被允许。

Once we've done that, the original conditional operator becomes equivalent to 一旦我们完成了这一点,原始的条件运算符就相当于

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

Done. 完成。

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. 将条件运算符分别处理为按位运算符 - 您不会通过尝试合并它们来为自己做任何好处。

I think the OP is looking for a way to express things which would normally require a conditional in a branchless way. 我认为OP正在寻找一种表达通常需要以无分支方式进行条件化的方法的方法。 For instance (assuming unsigned x,y,z; and x is bounded by INT_MAX ): 例如(假设unsigned x,y,z;并且xINT_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. 并希望编译器生成等效的位掩码或cmov指令。 :-) :-)

At the very basic level it becomes electronics. 在最基本的层面上它变成了电子产品。 See this . 看到这个 I cannot think of any other application for your question. 我想不出你的问题的任何其他申请。

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; 例如: (getsomevalue() == 1) ? somepointer : NULL; (getsomevalue() == 1) ? somepointer : NULL; can be represented as somepointer & ~((unsigned)(getsomevalue()) - 1); 可以表示为somepointer & ~((unsigned)(getsomevalue()) - 1); assuming getsomevalue() only returns 1 or 0(aka BOOL) 假设getsomevalue()只返回1或0(又名BOOL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM