# Bitwise Operators in C/C++

In C, the following 6 operators are bitwise operators (work at bit-level)

- The
**& (bitwise AND)**in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. - The
**| (bitwise OR)**in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1. - The
**^ (bitwise XOR)**in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. - The
**<< (left shift)**in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. - The
**>> (right shift)**in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. - The
**~ (bitwise NOT)**in C or C++ takes one number and inverts all bits of it.

**Example:**

## C++

`#include <iostream>` `using` `namespace` `std;` `int` `main() {` ` ` `// a = 5(00000101), b = 9(00001001)` ` ` `int` `a = 5, b = 9;` ` ` `// The result is 00000001` ` ` `cout<<` `"a = "` `<< a <<` `","` `<< ` `" b = "` `<< b <<endl;` ` ` `cout << ` `"a & b = "` `<< (a & b) << endl;` ` ` `// The result is 00001101` ` ` `cout << ` `"a | b = "` `<< (a | b) << endl;` ` ` `// The result is 00001100` ` ` `cout << ` `"a ^ b = "` `<< (a ^ b) << endl;` ` ` `// The result is 11111010` ` ` `cout << ` `"~a = "` `<< (~a) << endl;` ` ` `// The result is 00010010` ` ` `cout<<` `"b << 1"` `<<` `" = "` `<< (b << 1) <<endl;` ` ` `// The result is 00000100` ` ` `cout<<` `"b >> 1 "` `<<` `"= "` `<< (b >> 1 )<<endl;` ` ` `return` `0;` `}` `// This code is contributed by sathiyamoorthics19` |

## C

`// C Program to demonstrate use of bitwise operators` `#include <stdio.h>` `int` `main()` `{` ` ` `// a = 5(00000101), b = 9(00001001)` ` ` `unsigned ` `char` `a = 5, b = 9;` ` ` `// The result is 00000001` ` ` `printf` `(` `"a = %d, b = %d\n"` `, a, b);` ` ` `printf` `(` `"a&b = %d\n"` `, a & b);` ` ` `// The result is 00001101` ` ` `printf` `(` `"a|b = %d\n"` `, a | b);` ` ` `// The result is 00001100` ` ` `printf` `(` `"a^b = %d\n"` `, a ^ b);` ` ` `// The result is 11111010` ` ` `printf` `(` `"~a = %d\n"` `, a = ~a);` ` ` `// The result is 00010010` ` ` `printf` `(` `"b<<1 = %d\n"` `, b << 1);` ` ` `// The result is 00000100` ` ` `printf` `(` `"b>>1 = %d\n"` `, b >> 1);` ` ` `return` `0;` ` ` `}` |

**Output**

a = 5, b = 9 a & b = 1 a | b = 13 a ^ b = 12 ~a = -6 b << 1 = 18 b >> 1 = 4

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

**Interesting facts about bitwise operators**

**The left shift and right shift operators should not be used for negative numbers**. If the second operand(which decides the number of shifts) is a negative number, it results in undefined behaviour in C. For example results of both 1 <<- 1 and 1 >> -1 is undefined. Also, if the number is shifted more than the size of the integer, the behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. Another thing is, NO shift operation is performed if additive-expression(operand that decides no of shifts) is 0. See this for more details.**Note:**In**C++**, this behavior is well-defined.**Interestingly!! The bitwise OR of two numbers is just the sum of those two numbers if there is no carry involved, otherwise you just add their bitwise AND.**Let’s say, we have a=5(101) and b=2(010), since there is no carry involved, their sum is just a|b. Now, if we change ‘a’ to 6 which is 110 in binary, their sum would change to a|b + a&b since there is a carry involved.**The bitwise XOR operator is the most useful operator from**a**technical interview perspective.**It is used in many problems. A simple example could be “Given a set of numbers where all elements occur an even number of times except one number, find the odd occurring number” This problem can be efficiently solved by just doing XOR to all numbers.

## C++

`#include <iostream>` `using` `namespace` `std;` `// Function to return the only odd` `// occurring element` `int` `findOdd(` `int` `arr[], ` `int` `n)` `{` ` ` `int` `res = 0, i;` ` ` `for` `(i = 0; i < n; i++)` ` ` `res ^= arr[i];` ` ` `return` `res;` `}` `// Driver Method` `int` `main(` `void` `)` `{` ` ` `int` `arr[] = { 12, 12, 14, 90, 14, 14, 14 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << ` `"The odd occurring element is "` `<< findOdd(arr, n);` ` ` `return` `0;` `}` `// This code is contributed by shivanisinghss2110` |

## C

`#include <stdio.h>` `// Function to return the only odd` `// occurring element` `int` `findOdd(` `int` `arr[], ` `int` `n)` `{` ` ` `int` `res = 0, i;` ` ` `for` `(i = 0; i < n; i++)` ` ` `res ^= arr[i];` ` ` `return` `res;` `}` `// Driver Method` `int` `main(` `void` `)` `{` ` ` `int` `arr[] = { 12, 12, 14, 90, 14, 14, 14 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `printf` `(` `"The odd occurring element is %d "` `,` ` ` `findOdd(arr, n));` ` ` `return` `0;` `}` |

**Output**

The odd occurring element is 90

**Time Complexity: **O(n)

**Auxiliary Space: **O(1)

The following are many other interesting problems using XOR operator.

- Find the Missing Number
- swap two numbers without using a temporary variable
- A Memory Efficient Doubly Linked List
- Find the two non-repeating elements.
- Find the two numbers with odd occurrences in an unsorted-array.
- Add two numbers without using arithmetic operators.
- Swap bits in a given number.
- Count number of bits to be flipped to convert a to b .
- Find the element that appears once.
- Detect if two integers have opposite signs.

**The bitwise operators should not be used in place of logical operators.** The result of logical operators (&&, || and !) is either 0 or 1, but bitwise operators return an integer value. Also, the logical operators consider any non-zero operand as 1. For example, consider the following program, the results of & and && are different for same operands.

## C++

`#include <iostream>` `using` `namespace` `std;` `int` `main()` `{` ` ` `int` `x = 2, y = 5;` ` ` `(x & y) ? cout <<` `"True "` `: cout <<` `"False "` `;` ` ` `(x && y) ? cout <<` `"True "` `: cout <<` `"False "` `;` ` ` `return` `0;` `}` `// This code is contributed by shivanisinghss2110` |

## C

`#include <stdio.h>` `int` `main()` `{` ` ` `int` `x = 2, y = 5;` ` ` `(x & y) ? ` `printf` `(` `"True "` `) : ` `printf` `(` `"False "` `);` ` ` `(x && y) ? ` `printf` `(` `"True "` `) : ` `printf` `(` `"False "` `);` ` ` `return` `0;` `}` |

**Output**

False True

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

**The left-shift and right-shift operators are equivalent to multiplication and division by 2 respectively.** As mentioned in point 1, it works only if numbers are positive.

## C++

`#include <iostream>` `using` `namespace` `std;` `int` `main() {` ` ` `int` `x = 19;` ` ` `cout<<` `"x << 1 = "` `<< (x << 1) <<endl;` ` ` `cout<<` `"x >> 1 = "` `<< (x >> 1) <<endl;` ` ` `return` `0;` `}` `// This code is contributed by sathiyamoorthics19` |

## C

`#include <stdio.h>` `int` `main()` `{` ` ` `int` `x = 19;` ` ` `printf` `(` `"x << 1 = %d\n"` `, x << 1);` ` ` `printf` `(` `"x >> 1 = %d\n"` `, x >> 1);` ` ` `return` `0;` `}` |

**Output**

x << 1 = 38 x >> 1 = 9

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

**The & operator can be used to quickly check if a number is odd or even. **The value of expression (x & 1) would be non-zero only if x is odd, otherwise the value would be zero.

## C++

`#include <iostream>` `using` `namespace` `std;` `int` `main() {` ` ` `int` `x = 19 ;` ` ` `(x & 1) ? cout<<` `"Odd"` `: cout<< ` `"Even"` `;` ` ` ` ` `return` `0;` `}` `// This code is contributed by sathiyamoorthics19` |

## C

`#include <stdio.h>` `int` `main()` `{` ` ` `int` `x = 19;` ` ` `(x & 1) ? ` `printf` `(` `"Odd"` `) : ` `printf` `(` `"Even"` `);` ` ` `return` `0;` `}` |

**Output**

Odd

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

**The ~ 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)

## C++

`#include <iostream>` `using` `namespace` `std;` `int` `main() {` ` ` `unsigned ` `int` `x = 1;` ` ` `signed` `int` `a = 1;` ` ` `cout<<` `"Signed Result "` `<< ~a <<endl ;` ` ` `cout<<` `"Unsigned Result "` `<< ~x ;` ` ` `return` `0;` `}` `// This code is contributed by sathiyamoorthics19` |

## C

`// Note that the output of the following` `// program is compiler dependent` `#include <stdio.h>` `int` `main()` `{` ` ` `unsigned ` `int` `x = 1;` ` ` `printf` `(` `"Signed Result %d \n"` `, ~x);` ` ` `printf` `(` `"Unsigned Result %ud \n"` `, ~x);` ` ` `return` `0;` `}` |

**Output**

Signed Result -2 Unsigned Result 4294967294

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

- Bits manipulation (Important tactics)
- Bitwise Hacks for Competitive Programming
- Bit Tricks for Competitive Programming