Bitwise Operators in C#

HomeC#Featured

Bitwise Operators in C#

Get contents of HTML style tag in ASP.NET using C#
Hashing in C#
Regex in C#

Bitwise operators manipulate the individual bits of integer values. They work directly on the binary representation of numbers. Here’s a table summarizing the operators:

OperatorNameDescription
&Bitwise ANDPerforms a bitwise AND operation. If both corresponding bits are 1, the resulting bit is 1; otherwise, it’s 0.
`\`Bitwise OR
^Bitwise XORPerforms a bitwise exclusive OR (XOR) operation. If the corresponding bits are different, the resulting bit is 1; otherwise, it’s 0.
~Bitwise NOTPerforms a bitwise complement (NOT) operation, flipping all the bits.
<<Left ShiftShifts the bits of a value to the left by a specified number of positions, filling vacated positions with 0s.
>>Right ShiftShifts the bits of a value to the right by a specified number of positions.
  1. Bitwise AND ( & )
int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011

int result = a & b; // Binary: 0001 (Decimal: 1)

Console.WriteLine(result); // Output: 1

Explanation:

  • For each corresponding bit position, the & operator produces a 1 only if both bits are 1.
  • 0101 & 0011 results in 0001.

Bitwise OR ( | )

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011

int result = a | b; // Binary: 0111 (Decimal: 7)

Console.WriteLine(result); // Output: 7

Explanation:

  • The | operator produces a 1 if at least one of the corresponding bits is 1.
  • 0101 | 0011 results in 0111.

Bitwise XOR ( ^ )

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011

int result = a ^ b; // Binary: 0110 (Decimal: 6)

Console.WriteLine(result); // Output: 6

Explanation:

  • The ^ operator produces a 1 if the corresponding bits are different.
  • 0101 ^ 0011 results in 0110.

Bitwise NOT ( ~ )

int a = 5; // Binary 0000 0101 (32 bit Integer)
int result = ~a; //Binary 1111 1010. Decimal -6
Console.WriteLine(result); //output -6.

Explanation

  • The ~ operator inverts all the bits. All 0’s become 1’s, and all 1’s become 0’s.
  • Due to the nature of 2’s complement in how negative numbers are handled, the output of ~5 is -6.

Left Shift ( << )

int a = 5;  // Binary: 0000 0101

int result = a << 2; // Binary: 0001 0100 (Decimal: 20)

Console.WriteLine(result); // Output: 20

Explanation:

  • The << operator shifts the bits of a two positions to the left.
  • This effectively multiplies a by 2 raised to the power of the shift amount (2<sup>2</sup> = 4).
  • 5 * 4 = 20.

Right Shift ( >> )

int a = 20; // Binary: 0001 0100

int result = a >> 2; // Binary: 0000 0101 (Decimal: 5)

Console.WriteLine(result); // Output: 5

Explanation:

  • The >> operator shifts the bits of a two positions to the right.
  • This effectively divides a by 2 raised to the power of the shift amount.
  • Integer division of 20/4 equals 5.

Common Use Cases

  • Flags and Bitmasks: Bitwise operations are commonly used to represent and manipulate sets of boolean flags. Each bit represents a different state.
  • Low-Level Hardware Interaction: When dealing with hardware, bitwise operators are essential for working with registers and memory-mapped I/O.
  • Efficient Calculations: Bit shifting can be a fast way to multiply or divide by powers of 2.
  • Cryptography: Bitwise operations are used extensively in cryptographic algorithms.
  • Image Processing: image manipulation often involves bitwise operations on pixel color values.

Key Points

  • Bitwise operators work on integers (int, uint, long, ulong, byte, sbyte, short, ushort).
  • Understand binary representation to fully grasp how these operators work.
  • Bit shifting can significantly impact the value of a number.

COMMENTS

DISQUS: