这是什么布尔回报是什么意思? [英] What does this boolean return mean?
问题描述
在codeReview 我贴code的工作一块,要求提示,以改善它。其中我是使用一个布尔方法来检查,如果一个ArrayList有偶数个指标(这是必需的)。这是code的建议:
On CodeReview I posted a working piece of code and asked for tips to improve it. One I got was to use a boolean method to check if an ArrayList had an even number of indices (which was required). This was the code that was suggested:
private static boolean isEven(int number)
{
return (number & 1) == 0;
}
正如我已经纠缠,对于很多帮助特定的用户,我已经决定它的时间我缠着多所社区!我真的不明白这是如何工作。该方法被调用,并采取ArrayList的大小作为参数(即ArrayList中有十个元素,数= 10)。
As I've already pestered that particular user for a lot of help, I've decided it's time I pestered the SO community! I don't really understand how this works. The method is called and takes the size of the ArrayList as a parameter (i.e. ArrayList has ten elements, number = 10).
我认识一个&放大器;
运行数量和1的比较,但我得到了后失去
I know a single &
runs the comparison of both number and 1, but I got lost after that.
我看了它的方式,跟它返回真,如果若干== 0
和 1 == 0
。我知道,第一是不正确的,而后者显然没有意义。任何人可以帮助我呢?
The way I read it, it is saying return true if number == 0
and 1 == 0
. I know the first isn't true and the latter obviously doesn't make sense. Could anybody help me out?
编辑:我也许应该补充一点,code不工作,如果你想知道
推荐答案
请记住,与&是按位运算。你也许是意识到了这一点,但它根据您所提出的问题的方式并不完全清楚我的。
Keep in mind that "&" is a bitwise operation. You are probably aware of this, but it's not totally clear to me based on the way you posed the question.
话虽这么说,理论的想法是,你有一些int,它可以通过一些系列1和0的位pssed前$ P $。例如:
That being said, the theoretical idea is that you have some int, which can be expressed in bits by some series of 1s and 0s. For example:
...10110110
在二进制的,因为它是基体2,每当数的按位版本在0结束时,它是偶数,并且当它在1结束它为奇数。
In binary, because it is base 2, whenever the bitwise version of the number ends in 0, it is even, and when it ends in 1 it is odd.
因此,做一个按位&安培;用1对上面是:
Therefore, doing a bitwise & with 1 for the above is:
...10110110 & ...00000001
当然,这是0,所以你可以说,原来的输入均匀。
Of course, this is 0, so you can say that the original input was even.
另外,考虑奇数。例如,添加1到我们上面了。然后
Alternatively, consider an odd number. For example, add 1 to what we had above. Then
...10110111 & ...00000001
时等于1,并且因此,不等于零。瞧。
Is equal to 1, and is therefore, not equal to zero. Voila.
这篇关于这是什么布尔回报是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!