C also provides a set of very useful Boolean operators which can be used to combine values. These are &, | and ^.

& means AND. We will use this operator a lot when we start to look at individual bits which may be present in signals from the hardware. We know that a given number is actually a pattern of bits. The AND operator is applied between two operands and will set a bit in the result if that bit is set in both of the operands. This is sometimes called a bitwise AND. It is not the same as the logical AND we will see later when we look at comparisons. As an example

    i = 1 & 3 ;

The value 1 is held as the least significant bit (bit 0) set. The value 3 is held as the bottom two bits (bit 1 and bit 0) set. If I and them together I will get the result that bit 0 is set, but bit 1 isn't. This means that in the above statement the variable i would be set to the value 1.

     0 0 0 0 0 0 1 - bits for value of 1
          &          AND operation
     0 0 0 0 0 1 1 - bits for value of 3
         gives
     0 0 0 0 0 0 1 - bits for value of 1

| means OR. We can use this when we want to build up a value out of a number of bits. The OR operator is applied between two operands and will set a bit in the result if that bit is set in either of the operands. As an example:

    i = 1 | 2 ;

The value 1 is held as the least significant bit (bit 0) set. The value 2 is held as bit 1 set. When we combine them with an OR we get a result which has both bit 0 and bit 1 set. This result has the value 3. The above line would therefore set i to the value of 3.

     0 0 0 0 0 0 1 - bits for value of 1
          |          OR operation
     0 0 0 0 0 1 0 - bits for value of 2
         gives
     0 0 0 0 0 1 1 - bits for value of 3

^ means exclusive OR (XOR). This is perhaps the most confusing operator. It works between two operands and sets the output if either of the bits are set but not if they both are.

Another way of looking at XOR is that the output bit is set if the input bits are different. The output bit is clear if the input bits are the same.

    i = 1 ^ 3 ;

If we first consider bit 0 we find that this bit is set in the value 3 and the value 1. This means that bit 0 of our result will be 0. However, when we look at bit 1 we find this is set for the value 3 but clear for the value 1. This means that our result will only have bit 1 set, which leads to a result of 2 from this operation.

     0 0 0 0 0 0 1 - bits for value of 1
          ^          XOR operation
     0 0 0 0 0 1 1 - bits for value of 3
         gives
     0 0 0 0 0 1 0 - bits for value of 2

You might not see much of a use for XOR, but it does have a number of applications. It can be used to "flip" a bit in that if I XOR a bit with 1 it has the effect of changing it to 0 if the value is 1 and 1 if the value is 0.

Next time I do the XOR the value changes back again. This property has been used to provide simple encoding of data. If I XOR a value with an encoding value it will change to a new value. If I encode the new value with the encoding value I get the original back.

This means that if you know my magic encoding value you will be able to get back my original message. We will most often use XOR to flip a bit from one state to another.

If you are not used to working in binary, a useful tool is the 'Ready reckoner' which converts decimal, binary and hexadecimal values (see the 'Ready reckoner' page in the 'PICmicro reference' chapter for more details).

Click here to open the 'Ready reckoner'.