C# Bitwise Operators Example – Easy Explanation & Real-World Code
Alright, let’s talk about Bitwise Operators in C#. These little guys help you perform operations at the binary level. Sounds scary? Don’t worry! You don’t need to be a math genius to understand them. 😃
Think of it like switching lights on and off. Just 0s and 1s working together to get things done. If you’ve ever struggled with optimizing code, these operators are your secret weapon. Let’s break it down with a C# Bitwise Operators example.
🔍 How Bitwise Operators Work in C#
Bitwise operators work at the binary level, meaning they manipulate the individual bits of numbers. Before performing bitwise operations, C# converts numbers (integers, chars, etc.) into binary.
Each bit in a number is either 0 or 1, and bitwise operators perform operations directly on those bits.
📝 C# Bitwise Operators Table
Operator | Name | Description | Example (Decimal) | Example (Binary) | Result (Decimal) |
---|---|---|---|---|---|
& | AND | Sets bit to 1 if both bits are 1 , otherwise 0 |
5 & 3 | 0101 & 0011 | 1 |
| | OR | Sets bit to 1 if at least one bit is 1 |
5 | 3 | 0101 | 0011 | 7 |
^ | XOR | Sets bit to 1 if bits are different, otherwise 0 |
5 ^ 3 | 0101 ^ 0011 | 6 |
~ | NOT | Inverts all bits (1 → 0, 0 → 1) | ~5 | ~0101 | -6 (Two’s Complement) |
<< | Left Shift | Moves bits left, filling with 0 |
5 << 1 | 0101 << 1 | 10 |
>> | Right Shift | Moves bits right, keeping sign bit | 5 >> 1 | 0101 >> 1 | 2 |
💡 Key Point
& (AND)
keeps only the common1
bits.| (OR)
keeps all1
bits from both numbers.^ (XOR)
flips bits where numbers differ.~ (NOT)
inverts all bits (watch out for negative values).<< (Left Shift)
doubles the number (a * 2^n
).>> (Right Shift)
halves the number (a / 2^n
).
📝 How to Get the Binary Value of a Character and Integer?
Get binary value of a char
Every character has an ASCII or Unicode value, which can be converted to binary:
char letter = 'A';
int asciiValue = letter; // Get ASCII value
Console.WriteLine($"ASCII of {letter}: {asciiValue}");
Console.WriteLine($"Binary of {letter}: {Convert.ToString(asciiValue, 2)}");
Output
ASCII of A: 65
Binary of A: 1000001
Here, A
(65 in decimal) is 1000001
in binary.
Get binary value of an Integer
To see the binary representation of an integer, you can use Convert.ToString(value, 2).
int number = 5;
Console.WriteLine(Convert.ToString(number, 2)); // Output: 101
Here, 5
is represented as 101
in binary.
🎯 Bitwise Calculation Step-by-Step
Let’s take two numbers: 5 (0101
) and 3 (0011
). Now, let’s apply bitwise operations:
1️⃣ AND (&)
- Bitwise AND Operator
The &
(bitwise AND) operator in C# checks each bit of two numbers. It keeps 1 only if both numbers have 1 in that spot. If either number has 0, the result will be 0 in that place.
✅ If both bits are 1, the result is 1.
❌ If either bit is 0, the result is 0.
- Rule: 1 & 1 = 1, otherwise 0
- Example:
5 & 3
(And) |
Binary Value | Integer | |||
---|---|---|---|---|---|
0 | 1 | 0 | 1 | → (5) |
|
& | 0 | 0 | 1 | 1 | → (3) |
0 | 0 | 0 | 1 | → (1) |
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
Console.WriteLine(5 & 3); // Output: 1
2️⃣ OR (|)
Bitwise OR Operator
The bitwise OR (|
) operator compares each bit of two numbers and sets the result bit to 1 if at least one of the corresponding bits is 1. If both bits are 0, the result bit remains 0.
✅ If at least one bit is 1, the result is 1.
❌ If both bits are 0, the result is 0.
- Rule:
- 1 | 0 = 1
- 1 | 1 = 1
- Example:
5 | 3
(OR) |
Binary Value | Integer | |||
---|---|---|---|---|---|
0 | 1 | 0 | 1 | → (5) |
|
| | 0 | 0 | 1 | 1 | → (3) |
0 | 1 | 1 | 1 | → (7) |
0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1
Console.WriteLine(5 | 3); // Output: 7
3️⃣ XOR (^)
- Bitwise XOR Operator
The ^
(bitwise XOR) operator compares two numbers bit by bit and follows these simple rules:
✅ If the bits are different (0 and 1 or 1 and 0), the result is 1.
❌ If the bits are the same (0 and 0 or 1 and 1), the result is 0.
- Rule:
- 1 ^ 1 = 0
- 1 ^ 0 = 1
- Example:
5 ^ 3
(XOR) |
Binary Value | Integer | |||
---|---|---|---|---|---|
0 | 1 | 0 | 1 | → (5) |
|
^ | 0 | 0 | 1 | 1 | → (3) |
0 | 1 | 1 | 0 | → (6) |
- 0 ^ 0 = 0
- 1 ^ 0 = 1
- 0 ^ 1 = 1
- 1 ^ 1 = 0
Console.WriteLine(5 ^ 3); // Output: 6
4️⃣ NOT (~)
- Bitwise NOT Operator
The ~
(bitwise NOT) operator flips all the bits of a number:
✅ 0 becomes 1
❌ 1 becomes 0
- Rule: Flips all bits
- Example:
~5
int a = 5; // 00000000 00000000 00000000 00000101 (binary)
int result = ~a;
Console.WriteLine(result); // Output: -6
Step-by-Step:
00000000 00000000 00000000 00000101 (5)
~ -----------------------------------
11111111 11111111 11111111 11111010 (-6 in two's complement)
👉 0 becomes 1
👉 1 becomes 0
Why is the result negative?
In C#, numbers are stored using two’s complement, so flipping all bits changes the sign of the number and adds -1
.
For example:
~5
gives-6
~2
gives-3
5️⃣ Left Shift (<<)
- Bitwise Left Shift Operator
The <<
(left shift) operator moves all bits to the left and fills the empty spaces with 0
.
✅ Each shift left doubles the number.
Example
int a = 5; // 00000101 (binary)
int result = a << 1; // Shift left by 1
Console.WriteLine(result); // Output: 10
Step-by-Step:
00000101 (5)
<< 1
------------
00001010 (10)
👉 Every bit moves left, and a 0
is added on the right.
👉 Shifting left by 1 is like multiplying by 2.
More Examples:
int x = 3; // 00000011 (3)
Console.WriteLine(x << 2); // 12 (multiplied by 4)
💡 3 << 2
shifts left 2 times, multiplying by 2² = 4
.
6️⃣ Right Shift (>>)
- Bitwise Right Shift Operator
The >>
(right shift) operator moves all bits to the right and removes bits on the right.
✅ Each shift right divides the number by 2.
Example
int a = 8; // 00001000 (binary)
int result = a >> 1; // Shift right by 1
Console.WriteLine(result); // Output: 4
Step-by-Step:
00001000 (8)
>> 1
------------
00000100 (4)
👉 Each bit moves right, and the leftmost bit is filled based on the number type.
👉 Shifting right by 1 is like dividing by 2.
More Examples:
int x = 16; // 00010000 (16)
Console.WriteLine(x >> 2); // 4 (divided by 4)
💡 16 >> 2
shifts right 2 times, dividing by 2² = 4
.
📌 Bitwise Operators in Action
Let’s see these operators in real code with output.
using System;
class Program
{
static void Main()
{
int a = 5, b = 3;
Console.WriteLine($"a = {a}, b = {b}");
Console.WriteLine($"a & b = {a & b}"); // AND
Console.WriteLine($"a | b = {a | b}"); // OR
Console.WriteLine($"a ^ b = {a ^ b}"); // XOR
Console.WriteLine($"~a = {~a}"); // NOT
Console.WriteLine($"a << 1 = {a << 1}"); // Left Shift
Console.WriteLine($"a >> 1 = {a >> 1}"); // Right Shift
}
}
🖥 Output:
a = 5, b = 3
a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10
a >> 1 = 2
Wrapping It Up – You Nailed It! 🎉
Alright, buddy, you made it through all the bitwise operators! That’s awesome! 🚀
I know, at first, these symbols (&
, |
, ^
, ~
, <<
, >>
) might have looked scary. But now? You’ve cracked the code! 🧠💡 These operators are like tiny tools that make computers work smarter. And guess what? You just leveled up! 🎮✨
So, how do you feel? Feeling like a bitwise wizard yet? 🧙♂️ Or are some parts still a bit fuzzy? 🤔 That’s totally fine! Everyone struggles at first. Even pros mess up sometimes! The trick is practice—play around with them, test different numbers, and see the magic happen!
By the way, did you notice? Left shift (<<
) is like multiplying, while right shift (>>
) is like dividing—cool shortcut, right? 😎 And XOR (^
)? That sneaky one flips bits in style! 🔄
Now, be honest—what’s your biggest struggle with bitwise operators? Is it remembering how they work? Or maybe figuring out where to use them? Tell me, I’m here to help! 🤗
Also, just for fun—try shifting 1
left four times (1 << 4
). What do you get? Drop your answer! Let’s see if you’re the next bitwise champion! 🏆🔥