Bitwise Operators: Dr. Lavika Goel MNIT Jaipur
Bitwise Operators: Dr. Lavika Goel MNIT Jaipur
• It is better to define only 8 bits since a bit can also store the
values 0 or 1.
• But the problem is that there is no C type which is 1 bit long
(char is the longer with 1 byte).
• Solution: define a char (8 bits) but refer to each bit separately.
• Bitwise operators, introduced by the C language, provide
one of its more powerful tools for using and manipulating
memory. They give the language the real power of a “low-
level language”.
What is BitwiseStructure?
• Accessing bits directly is fast and efficient,
especially if you are writing a real-time
application.
• A single bit cannot be accessed
directly, since it has no address of
its own.
• The language introduces the bitwise
operators, which help in manipulating a
single bit of a byte.
• bitwise operators may be used on integral
types only (unsigned types are preferable).
4
Bitwise Operators
& bitwise AND
| bitwise OR
^ bitwise XOR
~ 1’s complement
<< Shift left
>> Shift right
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
6
Bitwise Operators -Examples
7
Setting Bits
• How can we set a bit on or off?
• Manipulations on bits are enabled by mask
and bitwise operators.
• Bitwise OR of anything with 1 results in 1.
• Bitwise AND of anything with 0 results in 0.
Setting Bits