Bitwise operators in C can be very handy in writing simple and efficient codes to solve many logical requirements.

Just to get an overview of how the bitwise operators in C can solve logical problems easily, here we are giving two examples which are tested in Linux environment.

1. Find whether the given input number in even or odd.

2. Find the missing number from the series of natural numbers starting from 1.

Note: To make the code easily understandable, some of the common defensive programming techniques are omitted.

So let’s start with the coding,

.

.

**1. Find whether the given input number is even or odd.**

Here in this simple program, the input is read from the ‘stdin’ and read into the variable ‘input’. If the entered value is zero, we are jumping to ‘end’ and exits the program.

The code,
res = !(input & 1); is computing whether the given number is even or odd. Here the variable ‘res’ will be set to ‘TRUE’ if the variable ‘input’ is containing an even number. Since the last bit of even number will be zero, the ANDing of ‘input’ with 1 will result in zero (**(input & 1)**), thereby making the entire right hand side operation to ‘TRUE’ (**!(input & 1)**)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include "stdio.h" int main(void) { unsigned char res; signed int input; printf("Enter the number: "); scanf("%d", &input); if (!input) goto end; // If input is 0, end program res = !(input & 1); // 'res' will be TRUE if input is an even number res ? printf("EVEN Number\n") : printf("ODD Number\n"); end: return 0; } |

**2. Find the missing number from the series of natural numbers starting from 1.**

In this program we can find the missing number from the given list of natural numbers starting from 1. The input list of numbers is stored in the array variable ‘real_input’. The function ‘GetTheOneMissingNumber()’ will find out the missing number from the given list.

In this program we are performing XOR between all the real numbers starting from 1 to ‘count+1’ and saving it to the local variable ‘xor_res2’. Similarly all the elements in the given list ‘input_list’ are also XOR’ed and saved into the local variable ‘xor_res1’. A final XOR between the values of ‘xor_res2’ and ‘xor_res1’ will give the missing number.

*Computation explained,
xor_res1 = X1 ^ X2 ^ X3 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res2 = X1 ^ X2 ^ X3 ^ X4 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res1 ^ xor_res2 = (X1 ^ X1) ^ (X2 ^ X2) ^ (X3 ^ X3) ^ (X4) ^ (X5 ^ X5) ^… (X10 ^ X10) = *

**X4**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include "stdio.h" unsigned int GetTheOneMissingNumber(unsigned int input_list[], unsigned int count) { unsigned int xor_res1; // XOR result for given list unsigned int xor_res2; // XOR result for all numbers 1 to n unsigned int i; xor_res1 = input_list[0]; xor_res2 = 1; for (i = 1; i < (count + 1); i++) { if (i < count) { xor_res1 ^= input_list[i]; } xor_res2 ^= (i + 1); } return (xor_res1 ^ xor_res2); } int main(void) { unsigned int real_input[9] = {3, 2, 1, 7, 6, 5, 8, 9, 10}; // 4 is missing printf("Missing number is %d\n", GetTheOneMissingNumber(real_input, 9)); return 0; } |

## Anonymous

This is a good one!!

Finding the missing number using XOR is a cool implementation.

i think it is possible to solve many logical problems with bitwise operators 🙂 🙂

## Rubio

In the first example the source code line “res = !(input & 1);” can be re-written as “res = input & 1;” provided,

res ? printf(“ODD Number\n”) : printf(“EVEN Number\n”);

## Nicole

Examples are good…

The finding the missing number using bitwise operators is a new learning to me.

Expecting more such tips from this site….