mikroC

making it simple...

Bitwise Operators

mikroC - C Compiler for Microchip PIC microcontrollers

Use the bitwise operators to modify the 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; returns 1 if both bits are 1, oth-

9

erwise returns 0

 

 

 

 

 

bitwise (inclusive) OR; returns 1 if either or

9

both bits are 1, otherwise returns 0

 

 

 

 

 

^

bitwise exclusive OR (XOR); returns 1 if the

10

bits are complementary, otherwise 0

 

 

 

 

 

~

bitwise complement (unary); inverts each bit

10

 

 

 

>>

bitwise shift left; moves the bits to the left,

10

see below

 

 

 

 

 

<<

bitwise shift right; moves the bits to the right,

10

see below

 

 

 

 

 

Note: Operator & can also be the pointer reference operator. Refer to Pointers for more information.

Bitwise operators &, , and ^ perform logical operations on appropriate pairs of bits of their operands. For example:

0x1234

& 0x5678;

 

/* equals 0x1230 */

/* because ..

 

 

 

0x1234 :

0001

0010

0011

0100

0x5678 :

0101

0110

0111

1000

---------------------------------

&: 0001 0010 0011 0000

.. that is, 0x1230 */

 

 

page

 

MikroElektronika: Development tools - Books - Compilers

105