Bitwise and Shift Operators
Manipulate bits with &, |, ^, ~, and shifts.
Bitwise and Shift Operators is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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);
}
}Bitwise OR: |
| sets a bit to 1 when either bit is 1. 5 | 3 gives 111 = 7.
using System;
class Program {
static void Main() {
Console.WriteLine(5 | 3);
Console.WriteLine(8 | 1);
}
}Bitwise XOR: ^
^ sets a bit to 1 when the bits differ. 5 ^ 3 gives 110 = 6.
using System;
class Program {
static void Main() {
Console.WriteLine(5 ^ 3);
Console.WriteLine(7 ^ 7);
}
}Bitwise NOT: ~
~ flips every bit. Because of two-complement representation, ~n equals -(n + 1).
using System;
class Program {
static void Main() {
Console.WriteLine(~5);
Console.WriteLine(~0);
}
}Left Shift: <<
<< moves bits left, filling zeros on the right. Each shift by one doubles the value: 1 << 3 is 8.
using System;
class Program {
static void Main() {
Console.WriteLine(1 << 3);
Console.WriteLine(5 << 1);
}
}Right Shift: >>
>> moves bits right. For positive numbers each shift halves the value: 20 >> 2 is 5.
using System;
class Program {
static void Main() {
Console.WriteLine(20 >> 2);
Console.WriteLine(8 >> 1);
}
}Bit Masks
A mask isolates specific bits. AND a value with a mask to keep only those bits.
using System;
class Program {
static void Main() {
int value = 0b1011;
int mask = 0b0110;
Console.WriteLine(value & mask);
}
}Setting and Clearing Bits
Use | with a single-bit mask to set a bit, and & with the inverted mask to clear it.
using System;
class Program {
static void Main() {
int flags = 0;
flags |= (1 << 2);
Console.WriteLine(flags);
flags &= ~(1 << 2);
Console.WriteLine(flags);
}
}Checking a Bit
To test whether a bit is set, AND with its mask and compare to non-zero.
using System;
class Program {
static void Main() {
int flags = 0b0100;
bool isSet = (flags & (1 << 2)) != 0;
Console.WriteLine(isSet);
}
}Shift as Fast Multiply
Shifting is a fast way to multiply or divide by powers of two, common in performance-sensitive code.
using System;
class Program {
static void Main() {
int n = 7;
Console.WriteLine(n << 2);
Console.WriteLine(n >> 1);
}
}Quick Check
Test your bitwise knowledge.
Recap
Bitwise operators work bit by bit: & AND, | OR, ^ XOR, and ~ NOT. Shifts << and >> move bits left or right, multiplying or dividing by powers of two.
With masks you can set, clear, and test individual bits, the foundation for flags and low-level data work.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Bitwise and Shift Operators” lesson free?
Yes — the full text of “Bitwise and Shift Operators” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Bitwise and Shift Operators”?
Manipulate bits with &, |, ^, ~, and shifts. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Bitwise and Shift Operators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity