Professional Documents
Culture Documents
ANDHRA PRADESH
Name : M.Subramanyam
Designation : Senior Lecturer
Branch : Computer Engg.
Institute : Q.Q.Govt.Polytechnic,Hyderabad.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-304
Topic : Bitwise operators
Duration : 50 Min
Sub Topic : Bitwise operators
Teaching Aids : PPTs, Animation
CM304.82 1
Recap
What is an operator?
What are the different types of operators?
What are the arithmetic Operators?
What are the Relational Operators?
What are the different assignment Operators?
Are the above operators work on bits?
CM304.82 2
Objectives
CM304.82 3
Bit-wise operators
CM304.82 4
Bit-wise operators
Contd..
3 ^ Exclusive OR(XOR)
4 ~ One’s complement
6 ! Bitwise OR
Fig.1
CM304.82 5
Hierarchy
CM304.82 6
Truth Table
1 1 1 1 0
1 0 1 0 1
0 1 1 0 1
0 0 0 0 0
CM304.82 7
Left shift(<<)
CM304.82 8
Example
int i, j;
i = 13;
j = i << 2;
J=i<<2 0000000000110100 (b
)
Fig.3 (a) before shift (b) after shift
J value is 53
CM304.82 9
Right shift(>>)
CM304.82 10
Example
int i, j;
i = 13;
j = i >> 2;
J=i>>2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 (b)
CM304.82 12
Bitwise OR( | )
Syntax: Op1 | op2
i = 21; j = 56;
k = i | j;
i=21 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 (a)
J=56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 (b)
k=i | j 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 (c)
k value is 61 Fig .6
CM304.82 13
Bitwise XOR( ^ )
Syntax: op1 ^ op2
i = 21; j = 56;
k = i ^ j;
i=21 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 (a)
J=56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 (b)
k value is 45 Fig.7
CM304.82 14
Bitwise One’s complement( ~ )
Syntax: ~op1
i = 21;
k = ~i;
i=21 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 (a)
k=~i 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 (b)
Fig.8
k value is 65514
CM304.82 15
Example
CM304.82 16
Example
CM304.82 17
Summary
C provides left and right shift operators to shift
bits in a byte or word.
The &, |, ^ and ~ operators are used to perform
bitwise operations.
The bitwise operators are used to manipulate
data at bit level.
System programming can be developed.
CM304.82 18
Quiz
CM304.82 19
Quiz
CM304.82 20
Quiz
CM304.82 21
Quiz
CM304.82 22
Quiz
CM304.82 23
Quiz
CM304.82 24
Frequently Asked questions
CM304.82 25