User manual

Table Of Contents
240
mikoC PRO for PIC32
MikroElektronika
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 complement operator ~ which associates
from right to left.
Bitwise Operators Overview
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, otherwise 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, discards the
far right bit and if unsigned assigns 0 to the far left bit,
otherwise sign extends
11
Logical Operations on Bit Level
&
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
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
----------------------------
& : 0001 0010 0011 0000
.. that is, 0x1230 */