Bitwise Complement Operator (~ tilde)

10 Dec, 2021
Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java
The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1’s become 0’s and vice versa. The operator for the bitwise complement is ~ (Tilde).
Example:
Input: ~ 0000 0011Output: 1111 1100
Input: 1110 0111Output: 0001 1000
The bitwise complement operator should be used carefully. The result of ~ operator on a small number can be a big number if the result is stored in an unsigned variable. And the result may be a negative number if the result is stored in a signed variable (assuming that the negative numbers are stored in 2’s complement form where the leftmost bit is the sign bit).
Input: n = 2Binary form of 2 = 0010Bitwise complement operation on 2 = ~ 0010                                                          = 1101 1101 is equivalent to decimal value 13.
Expected output: 13Correct Output : -3
The compiler returns the 2’s complement of the input value.
C
C++
Java
Python3
C#
Javascript
// C program to implement// the above approach#include <stdio.h> // Driver codeint main(){  int n = 2;  printf("Bitwise complement of %d : %d",          n, ~n);  return 0;}
// C++ program to implement// the above approach#include <iostream>using namespace std; // Driver codeint main(){  int a = 2;  cout << "Bitwise complement of " <<           a << " : " << ~a;}
// Java program to implement// the above approachimport java.io.*; // Driver codeclass GFG{    public static void main (String[] args)    {        int a = 2;        System.out.println("Bitwise complement of " +                            a + " : " + ~a);    }}
# Python3 program to implement# the above approach # Driver coden = 2print("Bitwise complement of {n} :",       ~n)
// C# program to implement // the above approachusing System; class GFG{ static public void Main(){    int a = 2;         Console.WriteLine("Bitwise complement of " + a +                      " : " + ~a);}}
<script> // JavaScript program to implement // the above approach // Driver codelet a = 2;document.write("Bitwise complement of " +               a + " : " + ~a); // This code is contributed by Potta Lokesh </script>
Output:
Bitwise complement of 2 : -3
Explanation:The bitwise complement of 2 (~2) is -3 instead of 13, but why? When numbers are printed in base-10, the result of a NOT operation can be surprising. In particular, positive numbers can become negative and vice versa.
Let’s first find the binary representation of bitwise complement of 2 which is -3
The negative numbers are stored as the two’s complement of the positive counterpart.
2’s Complement:Two’s complement is an operation on binary numbers. The 2’s complement of a number is equal to the complement of that number plus 1. 
Example:
Bitwise complement Operation of 2 (~ 0010 ): 1101
Calculate 2’s complement of 3: Binary form of 3 = 0011 1’s Complement of 3 = 1100 Adding 1 to 1’s complement = 1100 +12’s complement of 3 = 1101  
Note: The bitwise Complement of 2 is same as  the binary representation of -3 
Thus it can be concluded from the above example that-
For any integer n, the bitwise complement of n will be -(n+1).Bitwise complement of N = ~N (represented in 2’s complement form).2’complement of ~N= -(~(~N)+1) = -(N+1).
For any integer n, the bitwise complement of n will be -(n+1).
Bitwise complement of N = ~N (represented in 2’s complement form).
2’complement of ~N= -(~(~N)+1) = -(N+1).
lokeshpotta20
C-Operators
cpp-operator
Java-Operators
Bit Magic
C Language
C++
Java
Programming Language
Python
cpp-operator
Java-Operators
Bit Magic
Java
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.