# How to Check if a Number is Even or Odd using AND bitwise operation in C

In this article, we go over how to check if a number is even or odd using AND bitwise operation in C.

Embedded C is a language in C where it's important to do testing of bits. Many times, you need to check to see whether a bit is a 1 or a 0.

One way to test a bit to check whether it's a 1 or 0 is using AND bitwise operation.

Understanding bitwise operations requires to know the truth table for AND bitwise operations.

The truth table for a bitwise operation is shown below.

AND
bitwise operation |
||

Input |
Input |
Output |

0 |
0 |
0 |

0 |
1 |
0 |

1 |
0 |
0 |

1 |
1 |
1 |

The AND bitwise is good for testing bits because we know the output is 1 only if both inputs are 1. Otherwise, it will be 0.

Therefore, we can use the following code below to check whether a number is even or odd.

One thing to note is that when you are using a bitwise operation with a number, it does
the operation on the least significant bit, which is what we want in this case. We want to test the
last number of a number, which is the number that determines whether the number is even or odd.

So we create 4 32-bit numbers in the variables, num1, num2, num3, and num4.

How we check the numbers to see if they are even or odd is the if statement.

& is the AND bitwise operator. It allows us to do the AND operation with bits in C.

Because the AND operator only gives an output of 1 when both inputs are 1, we know that this if statement will only be true (or 1) when the last number is 1, which means the number is odd. Remember that bits are binary in nature. You need to convert all the numbers shown in the code to binary. Odd numbers always end with 1. And even numbers always end with 0.

The output from this program is shown below.

So this is how we can check if a number is even or odd using AND bitwise operation in C.