Datasheet

----------------------------
& : 0001 0010 0011 0000
.. that is, 0x1230 */
/* 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 posi-
tions 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 divid-
ing it by 2
n
.
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 */
199
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5