Check if a Number Is Odd or Even in Java – 在Java中检查一个数字是奇数还是偶数

最后修改: 2022年 6月 25日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

As we know, the parity of a number is determined by the remainder of its division by 2. Even numbers generate a remainder of 0, while odd numbers generate a remainder of 1.

我们知道,一个数字的奇偶性是由它除以2的余数决定的偶数产生的余数为0,而奇数产生的余数为1。

In this tutorial, we’ll see multiple ways to check whether a number is even or odd in Java.

在本教程中,我们将看到在Java中检查一个数字是偶数还是奇数的多种方法。

2. Division Approach

2.师的方法

The arithmetic operator which returns the division remainder is the modulus operator %.

返回除法余数的算术运算符是模数运算符%

The easiest way we can verify if a number is even or odd is by making the mathematical operation of dividing the number by 2 and checking the remainder:

我们验证一个数字是偶数还是奇数的最简单方法是进行数学运算,即用数字除以2并检查余数。

boolean isEven(int x) {
    return x % 2 == 0;
}

boolean isOdd(int x) {
    return x % 2 != 0;
}

Let’s write a couple of tests to confirm the behavior of our methods:

让我们写几个测试来确认我们方法的行为。

assertEquals(true, isEven(2));
assertEquals(true, isOdd(3));

3. Bitwise Operations Approach

3.位操作法

There are multiple bitwise operations that we can do on a number to determine if it’s even or odd.

有多种bitwise操作,我们可以对一个数字进行操作以确定它是偶数还是奇数。

The bitwise operations are more performant than the other methods to determine the parity of a number.

位操作是性能更强的比其他确定数字奇偶性的方法。

3.1. Bitwise OR (|)

3.1 位法OR (|)

An even number OR 1 will always increment the number by 1.

一个偶数OR1总是会使数字增加1

An odd number OR 1 will always result in the same number:

奇数OR1的结果总是 相同的数字

boolean isOrEven(int x) {
    return (x | 1) > x;
}

boolean isOrOdd(int x) {
    return (x | 1) == x;
}

Let’s demonstrate the behavior of our code with some tests:

让我们通过一些测试来证明我们代码的行为。

assertEquals(true, isOrEven(4));
assertEquals(true, isOrOdd(5));

3.2. Bitwise AND (&)

3.2. 位法AND (&)

An even number AND 1 always results in a 0. On the other hand, an odd number AND 1 results in a 1:

一个偶数and1的结果总是0。另一方面,一个奇数and1结果是1

boolean isAndEven(int x) {
    return (x & 1) == 0;
}

boolean isAndOdd(int x) {
    return (x & 1) == 1;
}

We’ll confirm this behavior with a small test:

我们将通过一个小测试来确认这一行为。

assertEquals(true, isAndEven(6));
assertEquals(true, isAndOdd(7));

3.3. Bitwise XOR (^)

3.3 位法XOR^

Bitwise XOR is the optimal solution to check the parity of a number.

逐位XOR最佳解决方案,用于检查一个数字的奇偶性。

An even number XOR 1 always increases the number by 1, while an odd number XOR 1 always decreases it by 1:

一个偶数XOR1总是增加1,而一个n奇数XOR1总是减少1

boolean isXorEven(int x) {
    return (x ^ 1) > x;
}

boolean isXorOdd(int x) {
    return (x ^ 1) < x;
}

Let’s write some small tests to check our code:

让我们写一些小测试来检查我们的代码。

assertEquals(true, isXorEven(8));
assertEquals(true, isXorOdd(9));

4. Least Significant Bit (LSB)

4.最小有效位(LSB)

The last method that we are presenting is reading the least significant bit of the number.

我们介绍的最后一种方法是读取数字中最没有意义的bit

An even number’s least significant bit is always 0, while that of an odd number is always 1:

一个偶数的最小有效位 总是0,而奇数的最小有效位总是1:

boolean isLsbEven(int x) {
    return Integer.toBinaryString(x).endsWith("0");
}

boolean isLsbOdd(int x) {
    return Integer.toBinaryString(x).endsWith("1");
}

We’ll demonstrate this behavior with a few lines of code:

我们将用几行代码来演示这一行为。

assertEquals(true, isLsbEven(10));
assertEquals(true, isLsbOdd(11));

5. Conclusion

5.总结

In this article, we’ve learned multiple ways to check the parity of a number, i.e., whether it’s even or odd. We saw that the optimal solution for checking the parity is the bitwise XOR operation.

在这篇文章中,我们已经学习了多种检查数字奇偶性的方法,也就是检查它是偶数还是奇数。我们看到,检查奇偶校验的最佳方案是位数XOR操作

As always, the source code for the examples is available over on GitHub.

像往常一样,这些例子的源代码可以在GitHub上找到