C Programming Code Examples C > Bitwise Operators Code Examples C program to flip all bits of a binary number C program to flip all bits of a binary number Write a C program to input a 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. Logic to flip all bits of a number Flipping a bit means toggling or inverting the current bit status. If the current bit is set i.e. 1 than invert it to 0 and vice versa. To flip all bits of a binary number you can run loop from 0 to size of the integer and flip individual bit at a time. However, C language has given bitwise complement ~ operator for the purpose. Bitwise complement ~ evaluates complement of the operand bit. It evaluate to 1 if corresponding bit of the operand is 0 otherwise evaluate to 0. Therefore to flip all bits of a number say j, you can use ~j. /* C program to count flip all bits of a binary number using bitwise operator */ #include <stdio.h> int main() { int j, flippedNumber; /* Input number from user */ printf("Enter any number: "); scanf("%d", &j); flippedNumber = ~j; printf("Original number = %d (in decimal)\n", j); printf("Number after bits are flipped = %d (in decimal)", flippedNumber); return 0; }