Write a C program to input any number from user and flip all bits of the given number (in binary representation) using bitwise operator. How to flip all bits of a binary number using bitwise operator in C programming.

Input any number: 22

Output number after bits are flipped: -23 (in decimal)

Happy coding ;)

**Example:**Input any number: 22

Output number after bits are flipped: -23 (in decimal)

### Required knowledge:

Basic C programming, Bitwise operator### Logic:

Flipping bits of a binary number is simple and easy task in C. There is an operator provided by C language that handles the task of flipping bits or we may say complementing any number. C provides*complement*operator that flips all bits of the number and we will use the bitwise complement ~ operator to flip bits of a given number.### Program:

/** * C program to count flip all bits of a binary number using bitwise operator */ #include <stdio.h> int main() { int num, flippedNumber; //Reads a number from user printf("Enter any number: "); scanf("%d", &num); flippedNumber = ~num; printf("Original number = %d (in decimal)\n", num); printf("Number after bits are flipped = %d (in decimal)", flippedNumber); return 0; }

Output

Enter any number: 22

Original number = 22 (in decimal)

Number after bits are flipped = -23 (in decimal)

Original number = 22 (in decimal)

Number after bits are flipped = -23 (in decimal)

Happy coding ;)

### You may also like

- Bitwise operator programming exercises index.
- C program to check Least Significant Bit (LSB) of a number is set or not.
- C program to check Most Significant Bit (MSB) of a number is set or not.
- C program to get nth bit of a number.
- C program to set nth bit of a number.
- C program to clear nth bit of a number.
- C program to toggle nth 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 total number of zeros and ones in a binary number.
- C program to find reverse of any given number.
- C program to check whether a number is palindrome or not.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).