Complete Reference for Bitwise Operators in Programming/Coding
Last Updated : 28 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
There exists no programming language that doesn't use Bit Manipulations. Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions. There are different bitwise operations used in bit manipulation. These Bitwise Operators operate on the individual bits of the bit patterns. Bit operations are fast and can be used in optimizing time complexity.
The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0.
Truth table of AND operator
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y
/*package whatever //do not write package name here */importjava.io.*;classGFG{publicstaticvoidmain(String[]args){inta=7,b=4;intresult=a&b;System.out.println(result);}}// This code is contributed by lokeshmvs21.
Python3
a=7b=4result=a&bprint(result)# This code is contributed by akashish__
C#
usingSystem;publicclassGFG{staticpublicvoidMain(){inta=7,b=4;intresult=a&b;Console.WriteLine(result);}}// This code is contributed by akashish__
JavaScript
leta=7,b=4;letresult=a&b;console.log(result);// This code is contributed by akashish__
Output
4
Time Complexity: O(1) Auxiliary Space: O(1)
2. Bitwise OR Operator (|)
The | Operator takes two equivalent length bit designs as boundaries; if the two bits in the looked-at position are 0, the next bit is zero. If not, it is 1.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise OR of both X, y
Bitwise OR of (7 | 4)
Explanation: On the basis of truth table of bitwise OR operator we can conclude that the result of
1 | 1 = 1 1 | 0 = 1 0 | 1 = 1 0 | 0 = 0
We used the similar concept of bitwise operator that are show in the image.
a=12b=25result=a|bprint(result)# This code is contributed by garg28harsh.
C#
usingSystem;publicclassGFG{staticpublicvoidMain(){inta=12,b=25;intresult=a|b;Console.WriteLine(result);}}// This code is contributed by akashish__
JavaScript
leta=12,b=25;letresult=a|b;document.write(result);// This code is contributed by garg28harsh.
Output
29
Time Complexity: O(1) Auxiliary Space: O(1)
3. Bitwise XOR Operator (^)
The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y
Bitwise OR of (7 ^ 4)
Explanation: On the basis of truth table of bitwise XOR operator we can conclude that the result of
1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0
We used the similar concept of bitwise operator that are show in the image.
importjava.io.*;classGFG{publicstaticvoidmain(String[]args){inta=12,b=25;intresult=a^b;System.out.println(result);}}// This code is contributed by garg28harsh.
Python3
a=12b=25result=a^bprint(result)# This code is contributed by garg28harsh.
C#
// C# CodeusingSystem;publicclassGFG{staticpublicvoidMain(){// Codeinta=12,b=25;intresult=a^b;Console.WriteLine(result);}}// This code is contributed by lokesh
JavaScript
leta=12;letb=25;console.log((a^b));// This code is contributed by akashish__
Output
21
Time Complexity: O(1) Auxiliary Space: O(1)
4. Bitwise NOT Operator (!~)
All the above three bitwise operators are binary operators (i.e, requiring two operands in order to operate). Unlike other bitwise operators, this one requires only one operand to operate.
The bitwise Not Operator takes a single value and returns its one’s complement. The one’s complement of a binary number is obtained by toggling all bits in it, i.e, transforming the 0 bit to 1 and the 1 bit to 0.
Truth Table of Bitwise Operator NOT
Example:
Take two bit values X and Y, where X = 5= (101)2 . Take Bitwise NOT of X.
Explanation: On the basis of truth table of bitwise NOT operator we can conclude that the result of
~1 = 0 ~0 = 1
We used the similar concept of bitwise operator that are show in the image.
Implementation of NOT operator:
C++
#include<iostream>usingnamespacestd;intmain(){inta=0;cout<<"Value of a without using NOT operator: "<<a;cout<<"\nInverting using NOT operator (with sign bit): "<<(~a);cout<<"\nInverting using NOT operator (without sign bit): "<<(!a);return0;}
Java
/*package whatever //do not write package name here */importjava.io.*;classGFG{publicstaticvoidmain(String[]args){inta=0;System.out.println("Value of a without using NOT operator: "+a);System.out.println("Inverting using NOT operator (with sign bit): "+(~a));if(a!=1)System.out.println("Inverting using NOT operator (without sign bit): 1");elseSystem.out.println("Inverting using NOT operator (without sign bit): 0");}}// This code is contributed by lokesh.
Python3
a=0print("Value of a without using NOT operator: ",a)print("Inverting using NOT operator (with sign bit): ",(~a))print("Inverting using NOT operator (without sign bit): ",int(not(a)))# This code is contributed by akashish__
C#
usingSystem;publicclassGFG{staticpublicvoidMain(){inta=0;Console.WriteLine("Value of a without using NOT operator: "+a);Console.WriteLine("Inverting using NOT operator (with sign bit): "+(~a));if(a!=1)Console.WriteLine("Inverting using NOT operator (without sign bit): 1");elseConsole.WriteLine("Inverting using NOT operator (without sign bit): 0");}}// This code is contributed by akashish__
JavaScript
leta=0;document.write("Value of a without using NOT operator: "+a);document.write("Inverting using NOT operator (with sign bit): "+(~a));if(!a)document.write("Inverting using NOT operator (without sign bit): 1");elsedocument.write("Inverting using NOT operator (without sign bit): 0");
Output
Value of a without using NOT operator: 0
Inverting using NOT operator (with sign bit): -1
Inverting using NOT operator (without sign bit): 1
Time Complexity: O(1) Auxiliary Space: O(1)
5. Left-Shift (<<)
The left shift operator is denoted by the double left arrow key (<<). The general syntax for left shift is shift-expression << k. The left-shift operator causes the bits in shift expression to be shifted to the left by the number of positions specified by k. The bit positions that the shift operation has vacated are zero-filled.
Note: Every time we shift a number towards the left by 1 bit it multiply that number by 2.
Logical left Shift
Example:
Input: Left shift of 5 by 1. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 1)
Left shift of 5 by 1
Output: 10 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010102, Which is equivalent to 10
Input: Left shift of 5 by 2. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 2)
Left shift of 5 by 2
Output: 20 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 101002, Which is equivalent to 20
Input: Left shift of 5 by 3. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 3)
Left shift of 5 by 3
Output: 40 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010002, Which is equivalent to 40
/*package whatever //do not write package name here */importjava.io.*;classGFG{publicstaticvoidmain(String[]args){intnum1=1024;Stringbt1=Integer.toBinaryString(num1);bt1=String.format("%32s",bt1).replace(' ','0');System.out.println(bt1);intnum2=num1<<1;Stringbt2=Integer.toBinaryString(num2);bt2=String.format("%32s",bt2).replace(' ','0');System.out.println(bt2);intnum3=num1<<2;Stringbitset13=Integer.toBinaryString(num3);bitset13=String.format("%16s",bitset13).replace(' ','0');System.out.println(bitset13);}}// This code is contributed by akashish__
Python3
# Python code for the above approachnum1=1024bt1=bin(num1)[2:].zfill(32)print(bt1)num2=num1<<1bt2=bin(num2)[2:].zfill(32)print(bt2)num3=num1<<2bitset13=bin(num3)[2:].zfill(16)print(bitset13)# This code is contributed by Prince Kumar
C#
usingSystem;classGFG{publicstaticvoidMain(string[]args){intnum1=1024;stringbt1=Convert.ToString(num1,2);bt1=bt1.PadLeft(32,'0');Console.WriteLine(bt1);intnum2=num1<<1;stringbt2=Convert.ToString(num2,2);bt2=bt2.PadLeft(32,'0');Console.WriteLine(bt2);intnum3=num1<<2;stringbitset13=Convert.ToString(num3,2);bitset13=bitset13.PadLeft(16,'0');Console.WriteLine(bitset13);}}// This code is contributed by akashish__
JavaScript
// JavaScript code for the above approachletnum1=1024;letbt1=num1.toString(2).padStart(32,'0');console.log(bt1);letnum2=num1<<1;letbt2=num2.toString(2).padStart(32,'0');console.log(bt2);letnum3=num1<<2;letbitset13=num3.toString(2).padStart(16,'0');console.log(bitset13);
The right shift operator is denoted by the double right arrow key (>>). The general syntax for the right shift is "shift-expression >> k". The right-shift operator causes the bits in shift expression to be shifted to the right by the number of positions specified by k. For unsigned numbers, the bit positions that the shift operation has vacated are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.
Note: Every time we shift a number towards the right by 1 bit it divides that number by 2.
Logical Right Shift
Example:
Input: Left shift of 5 by 1. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 1)
Right shift of 5 by 1
Output: 10 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010102, Which is equivalent to 10
Input: Left shift of 5 by 2. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 2)
Right shift of 5 by 2
Output: 20 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 101002, Which is equivalent to 20
Input: Left shift of 5 by 3. Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 3)
Right shift of 5 by 3
Output: 40 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010002, Which is equivalent to 40
// Java code for the above approachclassGFG{publicstaticvoidmain(String[]args){intnum1=1024;Stringbt1=String.format("%32s",Integer.toBinaryString(num1)).replace(' ','0');System.out.println(bt1);intnum2=num1>>1;Stringbt2=String.format("%32s",Integer.toBinaryString(num2)).replace(' ','0');System.out.println(bt2);intnum3=num1>>2;Stringbitset13=String.format("%16s",Integer.toBinaryString(num3)).replace(' ','0');System.out.println(bitset13);}}// This code is contributed by ragul21
usingSystem;classProgram{staticvoidMain(){intnum1=1024;// Right shift by 1intnum2=num1>>1;// Right shift by 2intnum3=num1>>2;// Print binary representationsstringbt1=Convert.ToString(num1,2).PadLeft(32,'0');stringbt2=Convert.ToString(num2,2).PadLeft(32,'0');stringbitset13=Convert.ToString(num3,2).PadLeft(16,'0');Console.WriteLine(bt1);Console.WriteLine(bt2);Console.WriteLine(bitset13);}}
JavaScript
// JavaScript code for the above approachletnum1=1024;letbt1=num1.toString(2).padStart(32,'0');console.log(bt1);letnum2=num1>>1;letbt2=num2.toString(2).padStart(32,'0');console.log(bt2);letnum3=num1>>2;letbitset13=num3.toString(2).padStart(16,'0');console.log(bitset13);// akashish__
Bit operations are used for the optimization of embedded systems.
The Exclusive-or operator can be used to confirm the integrity of a file, making sure it has not been corrupted, especially after it has been in transit.
Bitwise operations are used in Data encryption and compression.
Bits are used in the area of networking, framing the packets of numerous bits which are sent to another system generally through any type of serial interface.
Digital Image Processors use bitwise operations to enhance image pixels and to extract different sections of a microscopic image.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.