Anda di halaman 1dari 25

# DEPARTMENT OF TECHNICAL EDUCATION

Name : M.Subramanyam
Designation : Senior Lecturer
Branch : Computer Engg.
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

## On completion of this period, you would be able

to know
 Bit-wise operators.
 Hierarchy of bitwise operators.
 Operations on bitwise operators.

CM304.82 3
Bit-wise operators

##  Used to manipulate data at bit level.

 Bitwise data manipulations are machine
dependent.
 Storing booleans efficiently.
 Communicating directly with hardware.
 Work on integer operands only.

CM304.82 4
Bit-wise operators
Contd..

1 >> Right shift

## 2 << Left shift

3 ^ Exclusive OR(XOR)

4 ~ One’s complement

6 ! Bitwise OR
Fig.1
CM304.82 5
Hierarchy

## SNo Operator Priority Associativity

1 ~ 1 right to left
2 <<,>> 2 left to right
3 & 3 left to right
4 ^ 4 left to right
5 | 5 left to right
Fig.2

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(<<)

##  Shifting all bits left by a certain amount.

 Syntax
 op1 << op2
 Shifts bits of op1 left by distance op2;
 Rightmost bits which are shifted filled with ‘0’
bits.

CM304.82 8
Example
int i, j;
i = 13;
j = i << 2;

## Binary equivalent of decimal 13

i=13 0000000000001101
(a)

J=i<<2 0000000000110100 (b
)
Fig.3 (a) before shift (b) after shift
J value is 53
CM304.82 9
Right shift(>>)

 Syntax
 op1 >> op2

##  Shifts bits of op1 right by distance op2;

 Leftmost bits which are shifted filled with ‘0’ bits.

CM304.82 10
Example
int i, j;
i = 13;
j = i >> 2;

## Binary equivalent of decimal 13

i=13 0000000000001101 (a)

J=i>>2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 (b)

## Fig .4 (a) before shift, (b) after shift

J is now 3
CM304.82 11
Bitwise AND(&)
Syntax: Op & Op2
int i = 21, j = 56;
int k = i & j;
(a)
i=21 0000000000010101

## k=i & j 0000000000010000 (c )

k value is 16 Fig .5

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=i ^ j 0000 000000101101(c)

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

a)8
b)32
c)64
d)4

CM304.82 19
Quiz

a)8
b)32
c)64
d)4

CM304.82 20
Quiz

a)4
b)8
c)2
d)5

CM304.82 21
Quiz

a)4
b)8
c)2
d)5

CM304.82 22
Quiz

a)10
b)7
c)2
d)4

CM304.82 23
Quiz

a)10
b)7
c)2
d)4

CM304.82 24