mikroC - C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
/* Similarly: */ |
|
|
|
| ||
0x1234 | 0x5678; | /* equals 0x567C */ | ||||
0x1234 | ^ | 0x5678; | /* | equals | 0x444C | */ |
~ 0x1234; | /* | equals | 0xEDCB | */ |
Bitwise Shift Operators
Binary operators << and >> move the bits of the left operand for a number of positions specified by the right operand, to the left or right, respectively. Right operand has to be positive.
With shift left (<<), left most bits are discarded, and “new” bytes on the right are assigned zeros. Thus, shifting unsigned operand to left by n positions is equivalent to multiplying it by 2n if all the discarded bits are zero. This is also true for signed operands if all the discarded bits are equal to sign bit.
000001 << 5; | /* equals 000040 */ |
0x3801 << 4; /* equals 0x8010, overflow! */
With shift right (>>), right most bits are discarded, and the “freed” bytes on the left are assigned zeros (in case of unsigned operand) or the value of the sign bit zeros (in case of signed operand). Shifting operand to right by n positions is equivalent to dividing it by 2n.
0xFF56 | >> | 4; | /* | equals | 0xFFF5 | */ |
0xFF56u | >> | 4; | /* | equals | 0x0FF5 | */ |
Bitwise vs. Logical
Be aware 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 | */ |
page |
|
106 | MikroElektronika: Development tools - Books - Compilers |
|