#73 – Bitwise Operators

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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers