Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Bitwise Operators

These operators are used to perform bit wise operations on the operands.  In other words, bitwise operators work on binary representation of the value.  Except NOT(~) operator all the remaining bitwise operators are binary operators.  Bitwise operators only work with integer data type not with real values.  Bitwise operators can work with numbers, variables and even conditions.

Operator Symbol

Operation

&

AND

|

OR

^

XOR

~

NOT/COMPLIMENT

>> 

RIGHT SHIFT

<< 

LEFT SHIFT

Bitwise AND (&)

Bitwise AND works similar to Logical AND in the evaluation but the difference is Bitwise AND works on the binary representation of operand.

Difference between Logical AND (&&) and Bitwise AND (&)

In the case of numbers or variables, Logical AND(&&) works on the boolean values(non-zero is treated as ‘1’) while Bitwise AND works on binary representation.

4&&7,  4&7

In the case of conditions, Logical AND(&&) works on the short-circuit AND while Bitwise AND works as regular AND evaluation.

a>b && b>c

a>b & b>c

&& is known as short-circuit operator since it may or may not evaluate 2nd argument based on the evaluation of its 1st argument.

Left Shift Operator

This is binary operator that moves each bit in the binary representation of given number towards left to specified number of positions.  The blank trailing positions will be filled with ‘0’ after left shift operation.

//example code how left shift operator works

#include

void main()

{

    int var=3;

    printf(“%d\n”,var);

    var=var<<1;

    printf(“%d”,var);           //output 6

}

Left shifting is equivalent to multiplication of left operand by 2 power right operand.

In the above example, 3<<1 means 3×21

Right Shift Operator

#include

void main()

{

    int var=3;

    printf(“%d\n”,var);

    var=var>>1;

    printf(“%d”,var);           //OUTPUT 1

}

Right shifting is equivalent to division of left operand by 2 power right operand.

In the above example 3>>1 means 3/21

You cannot copy content of this page