#73 – Bitwise Operators
August 29, 2010 1 Comment
C# supports various bitwise operators for integral types. These operators allow performing the following operations on sequences of bits: Complement, OR, AND, and XOR.
~ Operator – Complement
The ~ operator takes a single operand and performs a bitwise complement operation, flipping each bit.
uint u = ~0xFF00C3A5; // 0x00FF3C5A
| Operator – OR
The | operator takes two operands and performs a bitwise OR, i.e. output bit is 1 if either input bit is 1.
uint u = 0x00FF3333 | 0x0F0F5555; // 0x0FFF7777;
& Operator – AND
The & operator takes two operands and performs a bitwise AND, i.e. output bit is 1 if both input bits are 1.
uint u = 0x00FF3333 & 0x0F0F5555; // 0x000F1111
^ Operator – XOR
The ^ operator takes two operands and performs an exclusive OR operation, i.e. output bit is 1 if exactly one input bit is 1.
uint u = 0x00FF3333 ^ 0x0F0F5555; // 0x0FF06666