简体   繁体   English

简化此表达式

[英]Simplify this expression

Let a , b be positive integers with different values. ab为具有不同值的正整数。 Is there any way to simplify these expressions: 有没有办法简化这些表达式:

bool foo(unsigned a, unsigned b)
{
    if (a % 2 == 0)
      return (b % 2) ^ (a < b); // Should I write "!=" instead of "^" ?
    else      
      return ! ( (b % 2) ^ (a < b) ); // Should I write "(b % 2) == (a < b)"? 
}

I am interpreting the returned value as a boolean. 我将返回的值解释为布尔值。

How is it different from 它有什么不同

 (a%2)^(b%2)^(a<b)

which in turn is 反过来又是

 ((a^b)&1)^(a<b)

or, indeed 或者,确实

 ((a ^ b) & 1) != (a < b)

Edited to add: Thinking about it some more, this is just the xor of the first and last bits of (ab) (if you use 2's complement), so there is probably a machine-specific ASM sequence which is faster, involving a rotate instruction. 编辑添加:再考虑一下,这只是(ab)的第一位和最后一位的xor(如果你使用2的补码),所以可能有一个机器特定的ASM序列更快,涉及一个旋转指令。

As a rule of thumb, don't mix operators of different operator families. 根据经验,不要混合不同运营商系列的运营商。 You are mixing relational/boolean operators with bitwise operators, and regular arithmetic. 您正在使用按位运算符和常规算法混合关系/布尔运算符。

This is what I think you are trying to do, I'm not sure, since I don't understand the purpose of your code: it is neither readable nor self-explaining. 这是我认为你想要做的,我不确定,因为我不理解你的代码的目的:它既不可读也不自我解释。

bool result;
bool a_is_even = (a % 2) == 0;
bool b_is_even = (b % 2) == 0;

if (a_is_even == b_is_even) // both even or both odd
  result = a < b;
else
  result = a >= b;

return result;

If you are returning a truth value, a boolean, then your proposed changes do not change the semantics of the code. 如果要返回真值,布尔值,则建议的更改不会更改代码的语义。 That's because bitwise XOR, when used in a truth context, is the same as != . 这是因为在真实上下文中使用时,按位XOR与!=相同。

In my view your proposed changes make the code much easier to understand. 在我看来,您提出的更改使代码更容易理解。 Quite why the author thought bitwise XOR would be appropriate eludes me. 很明白为什么作者认为按位XOR是合适的。 I guess some people think that sort of coding is clever. 我想有些人认为这种编码很聪明。 I don't. 我不。

If you want to know the relative performance of the two versions, write a program and time the difference. 如果您想知道两个版本的相对性能,请编写一个程序并计算时间差异。 I'd be surprised if you could measure any difference between them. 如果你可以测量它们之间的任何差异,我会感到惊讶。 And I'd be equally surprised if these lines of code were your performance bottleneck. 如果这些代码行是你的性能瓶颈,我同样会感到惊讶。

I program in C# but I'd think about something like this: 我用C#编程,但我想到这样的事情:

return (a % 2 == 0) && ((b % 2) ^ (a < b)) return(a%2 == 0)&&((b%2)^(a <b))

Considering from you comments that '^' is equivalent to '==' 从你的评论中可以看出'^'相当于'=='

Since there is not much information around this issue, try this: 由于此问题的信息不多,请尝试以下方法:

int temp = (b % 2) ^ (a < b);
if (a % 2 == 0)
  return temp;
else      
  return !temp;  

This should be less code if the compiler hasn't optimized it already. 如果编译器尚未对其进行优化,那么这应该是更少的代码。

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

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