Bitwise Complement Operator (~ tilde)
Last Updated :
20 Aug, 2024
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 0011
Output: 1111 1100
Input: 1110 0111
Output: 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 = 2
Binary form of 2 = 0010
Bitwise complement operation on 2 = ~ 0010
= 1101
1101 is equivalent to decimal value 13.
Expected output: 13
Correct Output : -3
The compiler returns the 2's complement of the input value.
C++
// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 2;
cout << "Bitwise complement of " <<
a << " : " << ~a;
}
C
// C program to implement
// the above approach
#include <stdio.h>
// Driver code
int main()
{
int n = 2;
printf("Bitwise complement of %d : %d",
n, ~n);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
// Driver code
class GFG
{
public static void main (String[] args)
{
int a = 2;
System.out.println("Bitwise complement of " +
a + " : " + ~a);
}
}
Python
# Python3 program to implement
# the above approach
# Driver code
n = 2
print("Bitwise complement of {n} :",
~n)
C#
// C# program to implement
// the above approach
using System;
class GFG{
static public void Main()
{
int a = 2;
Console.WriteLine("Bitwise complement of " + a +
" : " + ~a);
}
}
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Driver code
let 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 +1
2'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). since [ -(~(~n)-1) = -(n+1) ]
- Bitwise complement of N = ~N (represented in 2's complement form).
- 2'complement of ~N= -(~(~N)+1) = -(N+1).
Similar Reads
bitset operator[] in C++ STL bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset. Syntax: bitset_operator[index] = value Parameter: The parameter index specifies the position at which the value is to be assigned. Return Value: The function returns the value to the bit at t
2 min read
Bitwise Operators in C++ In C+, Bitwise Operators are the operators that are used to perform bit-level operations on the integers. While performing these operations, integers are considered as sequences of binary digits. These operators are useful for low-level programming, system programming, and optimizing performance.C++
6 min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read
C++ Bitwise Operator Overloading Prerequisites: Operator OverloadingBitwise Operator in C/C++ In C++, there are a total of six bitwise operators. The six bitwise operators are bitwise AND (&), bitwise OR (|), bitwise XOR (^), left shift (<<), right shift (>>), and bitwise NOT (~). The & (bitwise AND) in C++ take
7 min read
Python Bitwise Operators Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.Note: Python bitwis
5 min read