Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

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
                


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