Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Go to Problems

# Bitwise Operators

As we discussed in the previous video, the numbers are stored in their binary representation in computers and every single digit 0 / 1 is called bit.
Most languages allow you to perform operations which are bitwise ( this statement will make much more sense when we look at the operator themselves ). It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations.

Â

1. Bitwise AND:

Syntax:

``A & B``

Values for bit combinations:

``````            a      b         a & b
------------------------
0      0         0
0      1         0
1      0         0
1      1         1``````

In other words, `a & b = 0` unless `a = 1 and b = 1`.

What does A & B mean:

A & B implies a & b for all corresponding bits of A and B.

So, lets say,

``````       A = 21 ( 10101 ) and B = 6  ( 110 )
A & B =
1 0 1 0 1
&   0 0 1 1 0
------------------
0 0 1 0 0  =  4. ``````
2. Bitwise OR:

Syntax :

``````A | B
``````

Values for bit combinations

``````            a      b      a | b
------------------------
0      0         0
0      1         1
1      0         1
1      1         1
``````

In other words, `a | b = 1` unless `a = 0 and b = 0`

What does A | B mean :

`A | B` implies `a | b` for all corresponding bits of A and B. So, lets say

``````       A = 21 ( 10101 ) and B = 6  ( 110 )
A | B =
1 0 1 0 1
|   0 0 1 1 0
------------------
1 0 1 1 1  =  23. ``````
3. Bitwise XOR:

Syntax :

``````A ^ B
``````

Values for bit a, b :

``````            a      b      a ^ b
------------------------
0      0         0
0      1         1
1      0         1
1      1         0``````

In other words, `a ^ b = 1` when a and b are different.

What does A ^ B mean :

A ^ B implies a ^ b for all corresponding bits of A and B. So, lets say

``````       A = 21 ( 10101 ) and B = 6  ( 110 )
A ^ B =
1 0 1 0 1
^   0 0 1 1 0
------------------
1 0 0 1 1  =  19.
``````
4. Bitwise NOT:

Syntax :

``~A``

Values for bit a :

``````                                a   |   ~a
-------|-------
0   |   1
1   |   0``````

It's the inverse of the bit.

What does ~A mean

~A implies inverting every single bit in A. So, lets say

Â  Â  Â  Â  Â  Â  A = 21 ( 10101 ) and A is a char ( 1 byte )

Â  Â  Â  Â  Â  Â  ~A =Â

Â  Â  Â  Â  Â  Â  Â  Â  0 0 0 1 0 1 0 1

Â  Â  Â  Â  Â  Â  Â  --------------------------

Â  Â  Â  Â  Â  Â  Â  Â  1 1 1 0 1 0 1 0 Â = -22 ( Sign bit is 1 ).Â

5. Right Shift Operators:

Syntax :

``A >> x``

What does AÂ Â» x mean :

`A >> x` implies shifting the bits of A to the right by x positions. The last x bits are lost this way.

Example : Lets say

``````                A = 29 ( 11101 ) and x = 2,
so A >> 2 means
0 0 1 1 1 0 1 >> 2
====  -> is lost
========== -----> this sequence of digit shifts to the right by 2 positions
----------------
0 0 0 0 1 1 1 = 7``````

`A >> x` is equal to division by pow(2, x). Think why.

6. Left shift operators:

Syntax :

``A << x``

What does A Â«Â x mean :

`A << x` implies shifting the bits of A to the left by x positions. The first x bits are lost this way. The last x bits have 0. Example : lets say

``````                A = 29 ( 11101 ) and x = 2,
so A << 2 means
0 0 1 1 1 0 1 << 2
=============  ------> this sequence of digit shifts to the left by 2 positions
----------------
1 1 1 0 1 0 0  = 116 ``````

`A << x` is equal to multiplication by pow(2, x). Think why. `1 << x` is equal to pow(2, x).

## Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

## Bit Manipulation Problems

Bit play
Problem Score Companies Time Status
Number of 1 Bits 200 8:47
Trailing Zeroes 200 14:49
Reverse Bits 225 23:50
Divide Integers 250 68:04
Different Bits Sum Pairwise 300 51:29
Bit tricks
Problem Score Companies Time Status
Min XOR value 200 37:42
Count Total Set Bits 200 63:58
Palindromic Binary Representation 200 60:51
XOR-ing the Subarrays! 200 33:32
Bit array
Problem Score Companies Time Status
Single Number 275 11:53
Single Number II 275 39:22

Problem Score Companies Time Status
Bit Flipping 200 21:30
Swap Bits 150 26:16
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *