.NET 1.1+

# C# Logical Bitwise Operators

*The ninth part of the C# Fundamentals tutorial extends upon the previous article dealing with C# Boolean operators. Boolean operations may also be carried out on integer representations of binary numbers. This article considers logical bitwise operators.*

### Bitwise XOR Operator

The *bitwise exclusive OR* (XOR) operator allows the programmer to toggle specific bits between their possible values of zero or one. A common use of the XOR bitwise operator is to provide basic reversible encryption of data. As the bitwise exclusive OR toggles some bits, performing the operation twice restores the original values. With a long key for encryption, this can be quite effective (though inadvisable for data that needs to be kept very secure.) See below for an example of an exclusive OR operation.

10111011 = 187 01010101 = 85 XOR 11101110 = 238

Each binary digit pair is affected individually by the XOR operation. Wherever the second operand has a zero bit, the corresponding bit from the first operand is copied into the resultant value. However, if we set a one bit this toggles the matching bit in the result.

To achieve a bitwise XOR operation we use the caret character (^). The following example illustrates toggling bits in an integer value to encode and then decode data.

int valueToEncode = 187; // 10111011 int keyMask = 85; // 01010101 // Encode int encoded = valueToEncode ^ keyMask ; // Result = 238 // Decode int decoded = encoded ^ keyMask; // Result = 187

### One's Complement (NOT) Operator

The final basic logical bitwise operator provides a NOT function similar to that of the Boolean NOT operator but performed on each individual bit. This is known as a *one's complement* operation. The binary example is as follows:

10111011 = 187 NOT 01000100 = 68

The bitwise NOT operator is a *unary operator*, as it includes single operand. Unlike the other bitwise operators, the bitwise version does not use the same symbol as the similar Boolean operator. To achieve a one's complement, the tilde character (~) is positioned to the left of the value to modify.

byte valueToComplement = 187; // 10111011 byte complement = (byte) ~valueToComplement; // Result = 68

In the above example you can see that the result of the NOT operation has been cast to a byte . This is because the operator returns an integer value, which cannot be assigned directly into a byte. Note also that I have used an unsigned byte. In a signed byte the highest order bit is used to determine if the value is negative and the remaining bits are adjusted for negative numbers using *two's complement notation*. Using signed values can therefore give results that are tricky to decipher.

## Compound Assignment Operators

In part seven of the C# Fundamentals tutorial I introduced the compound assignment operators for arithmetic functions. The same type of compound operator exists for the bitwise AND, OR and XOR operators. The syntax of the compound assignment operators is identical in nature to that of the arithmetic variety. Some examples follow.

// Initialise byte value = 240; // 11110000 // Clear bit 7 value &= 127; // Result = 112 = 01110000 // Set bit 0 value |= 1; // Result = 113 = 01110001 // Toggle bits 1, 3, 5 and 7 value ^= 170; // Result = 219 = 11011011

## Operator Precedence

We can now add the new operators to the operator precedence table:

Parentheses Operator |
---|

() |

Increment / Decrement Operators |

++(postfix) --(postfix) ++(prefix) --(prefix) |

Complement Operators |

! ~ |

Basic Arithmetic Operators |

* / % + - |

Equivalence Operators |

== != |

Logic / Bitwise Operators |

& ^ | && || |

Assignment Operator |

= |

Compound Assignment Operators |

*= /= %= += -= &= ^= |= |