Datasheet
BITWISE OPERATORS
Use the bitwise operators to modify individual bits of numerical operands.
Bitwise operators associate from left to right. The only exception is the bitwise com-
plement operator ~ which associates from right to left.
Bitwise Operators Overview
Logical Operations on Bit Level
Bitwise operators &, | and ^ perform logical operations on the appropriate pairs of bits
of their operands. Operator ~ complements each bit of its operand. For example:
0x1234 & 0x5678 /* equals 0x1230 */
/* because ..
0x1234 : 0001 0010 0011 0100
0x5678 : 0101 0110 0111 1000
198
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5
Operator Operation Precedence
&
bitwise AND; compares pairs of bits and returns 1 if
both bits are 1, otherwise returns 0
8
|
bitwise (inclusive) OR; compares pairs of bits and
returns 1 if either or both bits are 1, otherwise
returns 0
6
^
bitwise exclusive OR (XOR); compares pairs of bits
and returns 1 if the bits are complementary, other-
wise returns 0
7
~
bitwise complement (unary); inverts each bit 14
<<
bitwise shift left; moves the bits to the left, discards
the far left bit and assigns 0 to the far right bit.
11
>>
bitwise shift right; moves the bits to the right, dis-
cards the far right bit and if unsigned assigns 0 to
the far left bit, otherwise sign extends
11
&
0 1
0 0 0
1 0 1
|
0 1
0 0 1
1 1 1
^
0 1
0 0 1
1 1 0
~
0 1
1 0