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

Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a:

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your career...

Current Company *
Enter company name
Experience *

You're all set!

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Bit Manipulation

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

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.

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. 
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. 
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. 
Bitwise NOT :
Syntax :
~A
Values for bit a :
                                a   |   ~a
                             -------|-------
                                0   |   1
                                1   |   0
                           

Its 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 ). 

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
Bit tricks
Bit array
Problem Score Companies Time Status
Single Number 275 11:53
Single Number II 275 39:22

Additional Practice

Problem Score Companies Time Status
Bit Flipping 200
TCS
19:48
Swap Bits 150
HCL
23:07
Free Mock Assessment
Help us know you better for the best experience
Current Employer *
Enter company name
College you graduated from *
Enter university name
Phone Number *
OTP will be sent to this number for verification
+1
+1
Change Number
Edit
Resend OTP
By Continuing I agree to be contacted by Scaler in the future.
Already have an account? Log in