C语言 如何使用按位运算符检查整数是偶数还是奇数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5700911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:26:00  来源:igfitidea点击:

How do I check if an integer is even or odd using bitwise operators

cbitwise-operators

提问by brisk man

How do I check if an integer is even or odd using bitwise operators

如何使用按位运算符检查整数是偶数还是奇数

回答by T.J. Crowder

Consider what being "even" and "odd" means in "bit" terms. Since binary integer data is stored with bits indicating multiples of 2, the lowest-order bit will correspond to 20, which is of course 1, while all of the otherbits will correspond to multiples of 2 (21= 2, 22= 4, etc.). Gratuituous ASCII art:

考虑“位”术语中的“偶数”和“奇数”是什么意思。由于二进制整数数据是以表示 2 的倍数的位存储的,所以最低位将对应 2 0,当然是 1,而所有其他位将对应于 2 的倍数(2 1= 2, 2 2= 4 等)。无偿的 ASCII 艺术:

NNNNNNNN
||||||||
|||||||+?? bit 0, value =   1 (20)
||||||+??? bit 1, value =   2 (21)
|||||+???? bit 2, value =   4 (22)
||||+????? bit 3, value =   8 (23)
|||+?????? bit 4, value =  16 (24)
||+??????? bit 5, value =  32 (25)
|+???????? bit 6, value =  64 (26)
+????????? bit 7 (highest order bit), value = 128 (27) for unsigned numbers,
                 value = -128 (-27) for signed numbers (2's complement)

I've only shown 8 bits there, but you get the idea.

我在那里只展示了 8 位,但你明白了。

So you can tell whether an integer is even or odd by looking only at the lowest-order bit: If it's set, the number is odd. If not, it's even. You don't care about the other bits because they all denote multiples of 2, and so they can't make the value odd.

因此,您可以仅通过查看最低位来判断整数是偶数还是奇数:如果已设置,则数字为奇数。如果没有,它甚至。您不关心其他位,因为它们都表示 2 的倍数,因此它们不能使值成为奇数。

The wayyou look at that bit is by using the AND operator of your language. In C and many other languages syntactically derived from B (yes, B), that operator is &. In BASICs, it's usually And. You take your integer, AND it with 1 (which is a number with only the lowest-order bit set), and if the result is not equal to 0, the bit was set.

方式,你看该位是通过使用你的语言的AND运算符。在 C 和许多其他从 B 语法派生的语言中(是的,B),该运算符是&。在 BASIC 中,它通常是And. 您取整数,并将其与 1(这是一个仅设置最低位的数字),如果结果不等于 0,则设置该位。

I'm intentionally notactually giving the code here, not only because I don't know what language you're using, but because you marked the question "homework." :-)

我故意不在这里提供代码,不仅因为我不知道您使用的是什么语言,还因为您将问题标记为“作业”。:-)

回答by Jim Blackler

In C (and most C-like languages)

在 C(和大多数类似 C 的语言)中

if (number & 1) {
  // It's odd 
}

回答by T M

if (number & 1)
    number is odd
else // (number & 1) == 0
    number is even

For example, let's take integer 25, which is odd. In binary 25 is 00011001. Notice that the least significant bit b0is 1.

例如,让我们取整数 25,它是奇数。在二进制 25 中是00011001. 请注意,最低有效位b0是 1。

00011001    
00000001   (00000001 is 1 in binary)
       &
--------
00000001

回答by neontapir

Just a footnote to Jim's answer.

只是吉姆回答的脚注。

In C#, unlike C, bitwise AND returns the resulting number, so you'd want to write:

在 C# 中,与 C 不同,按位 AND 返回结果数字,因此您需要编写:

if ((number & 1) == 1) {
   // It's odd
}

回答by Aditya Goel

if(x & 1)                               // '&' is a bit-wise AND operator
    printf("%d is ODD\n", x);
else
    printf("%d is EVEN\n", x);

Examples:

例子:

    For 9:

      9 ->        1 0 0 1
      1 ->     &  0 0 0 1
      -------------------
      result->    0 0 0 1

So 9 AND 1 gives us 1, as the right most bit of every odd number is 1.

所以 9 AND 1 给我们 1,因为每个奇数的最右边位是 1。

     For 14:

       14 ->      1 1 1 0
       1  ->   &  0 0 0 1
       ------------------
       result->   0 0 0 0

So 14 AND 1 gives us 0, as the right most bit of every even number is 0.

所以 14 AND 1 给我们 0,因为每个偶数的最右边的位是 0。

回答by Pankaj Prakash

You can do it simply using bitwise AND &operator.

您只需使用按位 AND&运算符即可完成此操作。

if(num & 1)
{
    //I am odd number.
}

Read more over here - Checking even odd using bitwise operator in C

在此处阅读更多信息 -在 C 中使用按位运算符检查偶数

回答by rahulxkumar

Also in Java you will have to use if((number&1)==1){//then odd}, because in Java and C# like languages the intis not casted to boolean. You'll have to use the relational operators to return a booleanvalue i.e trueand falseunlike C and C++ like languages which treats non-zero value as true.

同样在 Java 中,您将不得不使用if((number&1)==1){//then odd},因为在 Java 和 C# 之类的语言中,int不会强制转换为boolean. 您必须使用关系运算符来返回一个boolean值,即truefalse不像 C 和 C++ 之类的语言将非零值视为true.

回答by Testing123

#include <iostream>
#include <algorithm>
#include <vector>

void BitConvert(int num, std::vector<int> &array){
    while (num > 0){
        array.push_back(num % 2);
        num = num / 2;
    }
}

void CheckEven(int num){
    std::vector<int> array;
    BitConvert(num, array);
    if (array[0] == 0)
        std::cout << "Number is even";
    else
        std::cout << "Number is odd";
}

int main(){
    int num;
    std::cout << "Enter a number:";
    std::cin >> num;

    CheckEven(num);
    std::cout << std::endl;

    return 0;
}