0Pricing
C# Academy · Lesson

Bitwise and Shift Operators

Manipulate bits with &, |, ^, ~, and shifts.

Bits and Binary

Integers are stored as bits. Bitwise operators act on those individual bits. C# offers &, |, ^, ~, <<, and >>.

using System;

class Program {
    static void Main() {
        int x = 5;
        Console.WriteLine(Convert.ToString(x, 2));
    }
}

Bitwise AND: &

& sets a bit to 1 only when both bits are 1. 5 & 3 compares 101 and 011 to give 001 = 1.

using System;

class Program {
    static void Main() {
        Console.WriteLine(5 & 3);
        Console.WriteLine(12 & 10);
    }
}

All lessons in this course

  1. Arithmetic and Assignment Operators
  2. Comparison and Logical Operators
  3. Bitwise and Shift Operators
  4. Operator Precedence and Associativity
← Back to C# Academy