Mikroelektronika MIKROE-738 Datenbogen
mikroC PRO for PIC32
MikroElektronika
241
/* Similarly: */
0x1234 | 0x5678; /* equals 0x567C */
0x1234 ^ 0x5678; /* equals 0x444C */
~ 0x1234; /* equals 0xEDCB */
Note: Operator
&
can also be a pointer reference operator. Refer to Pointers for more information.
Bitwise Shift Operators
Binary operators
<<
and
>>
move the bits of the left operand by a number of positions specified by the right operand,
to the left or right, respectively. Right operand has to be positive.
With shift left (
<<
), far left bits are discarded and “new” bits on the right are assigned zeroes. Thus, shifting unsigned
operand to the left by n positions is equivalent to multiplying it by 2n if all discarded bits are zero. This is also true for
signed operands if all discarded bits are equal to a sign bit.
000001 << 5; /* equals 000040 */
0x3801 << 4; /* equals 0x8010, overflow! */
With shift right (
>>
), far right bits are discarded and the “freed” bits on the left are assigned zeroes (in case of unsigned
operand) or the value of a sign bit (in case of signed operand). Shifting operand to the right by n positions is equivalent
to dividing it by 2n.
0xFF56 >> 4; /* equals 0xFFF5 */
0xFF56u >> 4; /* equals 0x0FF5 */
Bitwise vs. Logical
Do not forget of the principle difference between how bitwise and logical operators work. For example:
0222222 & 0555555; /* equals 000000 */
0222222 && 0555555; /* equals 1 */
~ 0x1234; /* equals 0xEDCB */
! 0x1234; /* equals 0 */