你可以使用num%2或num& 1来检查数字是否是偶数? [英] Would you use num%2 or num&1 to check if a number is even?

查看:393
本文介绍了你可以使用num%2或num& 1来检查数字是否是偶数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,至少有两种低级别的方法来确定给定的数字是否为偶数:

Well, there are at least two low-level ways of determining whether a given number is even or not:

 1. if (num%2 == 0) { /* even */ } 
 2. if ((num&1) == 0) { /* even */ }

我认为第二个选项要更加优雅和有意义,这是我通常使用的。但它不仅是味道的问题;实际性能可能会有所不同:通常,逐位操作(例如,逻辑和这里)比mod(或div)操作更有效。当然,你可能会说,一些编译器将能够优化它,无论如何,我同意...但有些不会。

I consider the second option to be far more elegant and meaningful, and that's the one I usually use. But it is not only a matter of taste; The actual performance may vary: usually the bitwise operations (such as the logial-and here) are far more efficient than a mod (or div) operation. Of course, you may argue that some compilers will be able to optimize it anyway, and I agree...but some won't.

另一点是,第二一个人可能有点难以理解经验较少的程序员。我会回答,如果这些程序员花费这么短的时间来理解这种语句,那么它可能只会使每个人受益。

Another point is that the second one might be a little harder to comprehend for less experienced programmers. On that I'd answer that it will probably only benefit everybody if these programmers take that short time to understand statements of this kind.

你觉得怎么样?

给定的两个片段只有在 num 是无符号整数或具有二进制补码表示的负数时才是正确的。 -

The given two snippets are correct only if num is either an unsigned int, or a negative number with a two's complement representation. - As some comments righfuly state.

推荐答案

如果你打算说一些编译器不会优化%2 ,那么您还应该注意,一些编译器使用一个1'补码表示形式的有符号整数。在该表示中,& 1 给出了负数的错误答案。

If you're going to say that some compilers won't optimise %2, then you should also note that some compilers use a ones' complement representation for signed integers. In that representation, &1 gives the wrong answer for negative numbers.

所以你想要什么 - 一些编译器是慢的代码,或者在一些编译器错误的代码?

So what do you want - code which is slow on "some compilers", or code which is wrong on "some compilers"? Not necessarily the same compilers in each case, but both kinds are extremely rare.

当然,如果 num ,那么就不一定是相同的编译器。 code>是一个无符号类型,或者是C99固定宽度整数类型之一( int8_t 等等,它们需要是2的补码),然后这不是一个问题。在这种情况下,我认为%2 更加优雅和有意义,并且& 1 可能有时候需要执行。例如,我认为CPython不做这个优化,并且完全解释的语言也是如此(虽然然后解析开销可能会缩小两个机器指令之间的差异)。我会有点惊讶地发现一个C或C ++编译器没有做到尽可能,因为它是一个没有脑子的发出指令,如果不是之前。

Of course if num is of an unsigned type, or one of the C99 fixed-width integer types (int8_t and so on, which are required to be 2's complement), then this isn't an issue. In that case, I consider %2 to be more elegant and meaningful, and &1 to be a hack that might conceivably be necessary sometimes for performance. I think for example that CPython doesn't do this optimisation, and the same will be true of fully interpreted languages (although then the parsing overhead likely dwarfs the difference between the two machine instructions). I'd be a bit surprised to come across a C or C++ compiler that didn't do it where possible, though, because it's a no-brainer at the point of emitting instructions if not before.

一般来说,我会说在C ++中,你完全是在编译器的优化能力。标准容器和算法有n层间接,大多数间接在编译器完成内联和优化后消失。一个体面的C ++编译器可以在早餐前处理具有常量值的算术,而一个非体面的C ++编译器不管你做什么,都会产生垃圾代码。

In general, I would say that in C++ you are completely at the mercy of the compiler's ability to optimise. Standard containers and algorithms have n levels of indirection, most of which disappears when the compiler has finished inlining and optimising. A decent C++ compiler can handle arithmetic with constant values before breakfast, and a non-decent C++ compiler will produce rubbish code no matter what you do.

这篇关于你可以使用num%2或num& 1来检查数字是否是偶数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆