本文翻译自:Which is better option to use for dividing an integer number by 2?
Which of the following techniques is the best option for dividing an integer by 2 and why? 以下哪种技术是将整数除以2的最佳选择,为什么?
Technique 1: 技巧1:
x = x >> 1;
Technique 2: 技术2:
x = x / 2;
Here x
is an integer. 这里x
是整数。
参考:https://stackoom.com/question/ioiF/哪个是用于将整数除以-的更好选项
Just use divide ( /
), presuming it is clearer. 只需使用divide( /
),假设它更清晰。 The compiler will optimize accordingly. 编译器将相应地进行优化。
Does the first one look like dividing? 第一个看起来像分裂吗? No. If you want to divide, use x / 2
. 不。如果要分割,请使用x / 2
。 Compiler can optimise it to use bit-shift if possible (it's called strength reduction), which makes it a useless micro-optimisation if you do it on your own. 如果可能的话,编译器可以对其进行优化以使用位移(称为强度降低),如果您自己进行,则会使其成为无用的微优化。
x / 2
is clearer, and x >> 1
is not much faster (according to a micro-benchmark, about 30% faster for a Java JVM). x / 2
更清晰, x >> 1
不是更快(根据微基准测试,Java JVM快约30%)。 As others have noted, for negative numbers the rounding is slightly different, so you have to consider this when you want to process negative numbers. 正如其他人所指出的那样,对于负数,舍入略有不同,所以当你想要处理负数时你必须考虑这个。 Some compilers may automatically convert x / 2
to x >> 1
if they know the number can not be negative (even thought I could not verify this). 一些编译器可能会自动将x / 2
转换为x >> 1
如果他们知道数字不能为负数(甚至认为我无法验证这一点)。
Even x / 2
may not use the (slow) division CPU instruction, because some shortcuts are possible , but it is still slower than x >> 1
. 即使x / 2
也可能不使用(慢)除法CPU指令,因为某些快捷键是可能的 ,但它仍然比x >> 1
慢。
(This is a C / C++ question, other programming languages have more operators. For Java there is also the unsigned right shift, x >>> 1
, which is again different. It allows to correctly calculate the mean (average) value of two values, so that (a + b) >>> 1
will return the mean value even for very large values of a
and b
. This is required for example for binary search if the array indices can get very large. There was a bug in many versions of binary search , because they used (a + b) / 2
to calculate the average. This doesn't work correctly. The correct solution is to use (a + b) >>> 1
instead.) (这是一个C / C ++问题,其他编程语言有更多的运算符。对于Java,还有无符号右移, x >>> 1
,这又是不同的。它允许正确计算两个的平均值(平均值)值,即使对于a
和b
非常大的值, (a + b) >>> 1
也将返回平均值。例如,如果数组索引可能变得非常大,则需要二进制搜索。这是一个错误。许多版本的二进制搜索 ,因为他们使用(a + b) / 2
来计算平均值。这不能正常工作。正确的解决方案是使用(a + b) >>> 1
代替。)
Use the operation that best describes what you are trying to do. 使用最能描述您要执行的操作的操作。
Note that they are not exactly equivalent. 请注意,它们并不完全等效。 They can give different results for negative integers. 它们可以为负整数提供不同的结果。 For example: 例如:
-5 / 2 = -2
-5 >> 1 = -3
(ideone) (ideone)
Which one is the best option and why for dividing the integer number by 2? 哪一个是最佳选择以及为什么将整数除以2?
Depends on what you mean by best . 取决于你最好的意思。
If you want your colleagues to hate you, or to make your code hard to read, I'd definitely go with the first option. 如果你想让你的同事讨厌你,或者让你的代码难以阅读,我肯定会选择第一个选项。
If you want to divide a number by 2, go with the second one. 如果要将数字除以2,请使用第二个数字。
The two are not equivalent, they don't behave the same if the number is negative or inside larger expressions - bitshift has lower precedence than +
or -
, division has higher precedence. 这两者不相等,如果数字为负数或在较大的表达式内,它们的行为不相同 - bitshift的优先级低于+
或-
,除法具有更高的优先级。
You should write your code to express what its intent is. 您应该编写代码来表达其意图。 If performance is your concern, don't worry, the optimizer does a good job at these sort of micro-optimizations. 如果您关心性能,请不要担心,优化器在这些微优化方面做得很好。