Write a C program to input any number and check whether the given number is even or odd using bitwise operator. How to check whether a number is even or odd using bitwise operator in C programming. How to use bitwise AND operator to check whether a number is even or odd in C programming.

Input number: 12

Output: 12 is even.

Also check this program using other methods -

C program to check even or odd using switch case.

C program to check even or odd using conditional operator.

C program to check even or odd using functions.

In the above image you can see that if the number is even then its LSB is 0 otherwise if the number is odd its LSB is 1. Hence we need to check LSB of the number. And to check the status of any bit you can use bitwise AND operator. If num & 1 evaluates to 1 then the number is odd otherwise even.

Happy coding ;)

**Example:**Input number: 12

Output: 12 is even.

Also check this program using other methods -

C program to check even or odd using switch case.

C program to check even or odd using conditional operator.

C program to check even or odd using functions.

### Required knowledge

Basic C programming, Bitwise operator, If else### Logic to check even or odd using bitwise operator

You already know how to check even or odd using modulus operator % but there also exists a fastest and simplest method for checking even or odd numbers using bitwise AND & operator in C. We know that the bitwise operator operates on bit level hence in-order to check whether a number is odd we only need to check if its Least Significant Bit (LSB) is set or not. If the 0^{th}bit of any number is set (1) then the number is odd otherwise even.### Program to check even or odd using bitwise operator

/** * C program to check even or odd number using bitwise operator */ #include <stdio.h> int main() { int num; //Reads a number from user printf("Enter any number: "); scanf("%d", &num); if(num & 1) { printf("%d is odd.", num); } else { printf("%d is even.", num); } return 0; }

**Note:**You can also use conditional operator to short the program as done below.

### Program to check even or odd using conditional and bitwise operator

/** * C program to check whether a number is even or odd using bitwise operator */ #include <stdio.h> int main() { int num; //Reads a number from user printf("Enter any number: "); scanf("%d", &num); (num & 1) ? printf("%d is odd.", num) : printf("%d is even.", num); return 0; }

Output

Enter any number: 15

15 is odd.

15 is odd.

Happy coding ;)

### You may also like

- Bitwise operator programming exercises index.
- C program to check even or odd using if else.
- C program to check even or odd using conditional operator.
- C program to check even or odd using switch case.
- C program to get lowest set bit of a number.
- C program to count trailing zeros in a binary number.
- C program to count leading zeros in a binary number.
- C program to flip bits of a binary number using bitwise operator.
- C program to total number of zeros and ones in a binary number.
- C program to convert decimal to binary number system using bitwise operator.
- C program to swap two numbers using bitwise operator.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)