Integral Bitwise Logical Operators

The available operators that deal with integral values—as usual defined for int, uint, long, and ulong—include the following:

&  |  ^  ~

These, respectively, correspond to the bitwise AND, OR, XOR (exclusive OR), and complement operations. The following are a few examples:

int x = 5;int y = 3;int and   = x & y; // 1int or    = x | y; // 7int xor   = x ^ y; // 6int compX = ~x;    // -6

Figure 5.20 illustrates the operation of those operators (restricting myself to 4-bit representations to save space).

Image

FIGURE 5.20 Integral bitwise operations.

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.