📌  相关文章
📜  使用按位运算符检查数字是奇数还是偶数

📅  最后修改于: 2021-04-29 16:36:08             🧑  作者: Mango

给定数字N ,任务是使用按位运算符检查数字是偶数还是奇数。
例子:

以下按位运算符可用于检查数字是奇数还是偶数:

1.使用按位XOR运算符
想法是检查数字的最后一位是否设置。如果设置了最后一位,则数字为奇数,否则为偶数。
如我们所知,如果数字为偶数,则按数字按位XOR运算会将数字的值加1;否则,如果值为奇数,则将数字的值减1。

下面是上述方法的实现:

C++
// C++ program to check for even or odd
// using Bitwise XOR operator
 
#include 
using namespace std;
 
// Returns true if n is even, else odd
bool isEven(int n)
{
 
    // n^1 is n+1, then even, else odd
    if (n ^ 1 == n + 1)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 100;
    isEven(n)
? cout << "Even"
: cout << "Odd";
 
    return 0;
}


Java
// Java program to check for even or odd
// using Bitwise XOR operator
class GFG
{
 
    // Returns true if n is even, else odd
    static boolean isEven(int n)
    {
 
        // n^1 is n+1, then even, else odd
        if ((n ^ 1) == n + 1)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.print(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to check for even or odd
# using Bitwise XOR operator
 
# Returns true if n is even, else odd
def isEven( n) :
 
    # n^1 is n+1, then even, else odd
    if (n ^ 1 == n + 1) :
        return True;
    else :
        return False;
 
# Driver code
if __name__ == "__main__" :
    n = 100;
    print("Even") if isEven(n) else print( "Odd");
 
# This code is contributed by AnkitRai01


C#
// C# program to check for even or odd
// using Bitwise XOR operator
using System;
 
class GFG
{
 
    // Returns true if n is even, else odd
    static bool isEven(int n)
    {
 
        // n^1 is n+1, then even, else odd
        if ((n ^ 1) == n + 1)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 100;
        Console.Write(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program to check for even or odd
// using Bitwise AND operator
#include 
using namespace std;
   
// Returns true if n is even, else odd
bool isEven(int n)
{
    // n&1 is 1, then odd, else even
    return (!(n & 1));
}
   
// Driver code
int main()
{
    int n = 101;
    isEven(n)
    ? cout << "Even"
    : cout << "Odd";
    return 0;
}


Java
// Java program to check for even or odd
// using Bitwise AND operator
class GFG
{
     
// Returns true if n is even, else odd
static boolean isEven(int n)
{
    // n&1 is 1, then odd, else even
    return ((n & 1)!=1);
}
     
// Driver code
public static void main(String[] args)
{
    int n = 101;
    System.out.print(isEven(n) == true ? "Even" : "Odd");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to check for even or odd
# using Bitwise AND operator
 
# Returns true if n is even, else odd
def isEven(n) :
 
    # n&1 is 1, then odd, else even
    return (not(n & 1));
 
# Driver code
if __name__ == "__main__" :
 
    n = 101;
    if isEven(n) :
        print("Even")
    else:
        print("Odd");
 
# This code is contributed by AnkitRai01


C#
// C# program to check for even or odd
// using Bitwise AND operator
using System;
 
class GFG
{
     
    // Returns true if n is even, else odd
    static bool isEven(int n)
    {
        // n&1 is 1, then odd, else even
        return ((n & 1) != 1);
    }
         
    // Driver code
    public static void Main()
    {
        int n = 101;
        Console.Write(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program to check for even or odd
// using Bitwise OR operator
 
#include 
using namespace std;
 
// Returns true if n is even, else odd
bool isEven(int n)
{
 
    // n|1 is greater than n, then even, else odd
    if ((n | 1) > n)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 100;
    isEven(n)
    ? cout << "Even"
    : cout << "Odd";
 
    return 0;
}


Java
// Java program to check for even or odd
// using Bitwise OR operator
class GFG
{
    // Returns true if n is even, else odd
    static boolean isEven(int n)
    {
 
        // n|1 is greater than n, then even, else odd
        if ((n | 1) > n)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.print(isEven(n) == true ? "Even" : "Odd");
    }
}


Python3
# Python3 program to check for even or odd
# using Bitwise OR operator
 
# Returns true if n is even, else odd
def isEven( n) :
 
    # n|1 is greater then n, then even, else odd
    if (n | 1 > n) :
        return True;
    else :
        return False;
 
# Driver code
if __name__ == "__main__" :
    n = 100;
    print("Even") if isEven(n) else print( "Odd");


C#
// C# program to check for even or odd
// using Bitwise XOR operator
using System;
 
class GFG
{
 
    // Returns true if n is even, else odd
    static bool isEven(int n)
    {
 
        // n|1 is greater then n, then even, else odd
        if ((n | 1) > n)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 100;
        Console.Write(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出
Even

2.使用按位AND运算符:
想法是检查数字的最后一位是否设置。如果设置了最后一位,则数字为奇数,否则为偶数。
众所周知,数字的按位与运算将为1,如果为奇数,因为最后一位已被设置,则为1。否则它将给出0作为输出。

下面是上述方法的实现:

C++

// C++ program to check for even or odd
// using Bitwise AND operator
#include 
using namespace std;
   
// Returns true if n is even, else odd
bool isEven(int n)
{
    // n&1 is 1, then odd, else even
    return (!(n & 1));
}
   
// Driver code
int main()
{
    int n = 101;
    isEven(n)
    ? cout << "Even"
    : cout << "Odd";
    return 0;
}

Java

// Java program to check for even or odd
// using Bitwise AND operator
class GFG
{
     
// Returns true if n is even, else odd
static boolean isEven(int n)
{
    // n&1 is 1, then odd, else even
    return ((n & 1)!=1);
}
     
// Driver code
public static void main(String[] args)
{
    int n = 101;
    System.out.print(isEven(n) == true ? "Even" : "Odd");
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 program to check for even or odd
# using Bitwise AND operator
 
# Returns true if n is even, else odd
def isEven(n) :
 
    # n&1 is 1, then odd, else even
    return (not(n & 1));
 
# Driver code
if __name__ == "__main__" :
 
    n = 101;
    if isEven(n) :
        print("Even")
    else:
        print("Odd");
 
# This code is contributed by AnkitRai01

C#

// C# program to check for even or odd
// using Bitwise AND operator
using System;
 
class GFG
{
     
    // Returns true if n is even, else odd
    static bool isEven(int n)
    {
        // n&1 is 1, then odd, else even
        return ((n & 1) != 1);
    }
         
    // Driver code
    public static void Main()
    {
        int n = 101;
        Console.Write(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by AnkitRai01

Java脚本


输出
Odd

3.使用按位或运算符:想法是检查数字的最后一位是否已设置。如果设置了最后一位,则数字为奇数,否则为偶数。如我们所知,如果数字是偶数,则对数字按位进行“或”运算会将数字的值加1,否则它将保持不变。因此,如果在对数字1进行“或”运算后得出的结果大于数字,则结果为偶数,我们将返回true,否则返回奇数,并且将返回false。

下面是上述方法的实现:

C++

// C++ program to check for even or odd
// using Bitwise OR operator
 
#include 
using namespace std;
 
// Returns true if n is even, else odd
bool isEven(int n)
{
 
    // n|1 is greater than n, then even, else odd
    if ((n | 1) > n)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 100;
    isEven(n)
    ? cout << "Even"
    : cout << "Odd";
 
    return 0;
}

Java

// Java program to check for even or odd
// using Bitwise OR operator
class GFG
{
    // Returns true if n is even, else odd
    static boolean isEven(int n)
    {
 
        // n|1 is greater than n, then even, else odd
        if ((n | 1) > n)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.print(isEven(n) == true ? "Even" : "Odd");
    }
}

Python3

# Python3 program to check for even or odd
# using Bitwise OR operator
 
# Returns true if n is even, else odd
def isEven( n) :
 
    # n|1 is greater then n, then even, else odd
    if (n | 1 > n) :
        return True;
    else :
        return False;
 
# Driver code
if __name__ == "__main__" :
    n = 100;
    print("Even") if isEven(n) else print( "Odd");

C#

// C# program to check for even or odd
// using Bitwise XOR operator
using System;
 
class GFG
{
 
    // Returns true if n is even, else odd
    static bool isEven(int n)
    {
 
        // n|1 is greater then n, then even, else odd
        if ((n | 1) > n)
            return true;
        else
            return false;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 100;
        Console.Write(isEven(n) == true ? "Even" : "Odd");
    }
}
 
// This code is contributed by Rajput-Ji

Java脚本


输出
Even

时间复杂度: O(1)