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
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity