Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Bitwise Operators in C Programming: AND, XOR, OR, Complement and Shift Operations - Codzify Tutorials

2 min 31 sec read

There are many Bitwise operators in C as defined below:

& (bitwise AND) :- Takes two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

| (bitwise OR) :- Takes two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR) :- Takes two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

<< (left shift) :- Takes two numbers, left shifts the bits of first operand, the second operand decides the number of places to shift.

>> (right shift) :- Takes two numbers, right shifts the bits of first operand, the second operand decides the number of places to shift.

~ (bitwise NOT) :- Takes one number and inverts all bits of it.

Rule :-

& = 1 & 1 is 1 else 0.

| = any of two bit is 1 then 1 else 0.

^ = if both bits are different then 1 else 0.

a>> 1 = shift the bits of a to right by 1 and replace the first bit from left by 0 .

a>> 2 = shift the bits of a to right by 2 and replace the first two bits from left by 0 .

a<< 1 = shift the bits of a to left by 1 and replace the last bit from right by 0 .

a<< 2 = shift the bits of a to left by 2 and replace the last two bits from right by 0 .

~a = replace 0 by 1 and 1 by 0.

Example :-

                a= 5    =    00000101
                b= 9    =    00001001
                覧覧覧覧覧覧覧覧覧覧
                a&b     =    00000001
                a|b     =    00001101
                a^b     =    00001100
                a>>1    =    00000100
                a>>1    =    00001010
                ~a      =    11111010
                


Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor