C# supports five main types of operators, which are grouped together based on the idea behind their existence.
Categories of operators:
The concept of operator precedence prescribes the logic for how complex expressions are evaluated. This differs by operator category. For a full list, visit the official Microsoft reference.
This guide will discuss bitwise and logical operators, including how you can understand their effect on your code.
This category of operators works with Boolean variables or expressions. There are three basic operands it allows you to use: AND
, OR
, and NOT
. Every complex logical expression can be built using a combination of these. A truth table can be quickly drawn up to verify the logic and the results, and below we will see the truth table of these operands.
Let's assume 1
means true
and 0
means false
.
The truth table of the AND
and OR
operands:
A | B | AND | OR |
---|---|---|---|
1 | 0 | 0 | 1 |
0 | 1 | 0 | 1 |
0 | 0 | 0 | 0 |
1 | 1 | 1 | 1 |
When you are working with the AND
operand, there is a very easy way to understand and calculate the results. The result is always false
as long as there is a 0
among the operands and true
when both operands are 1
.
Working with the OR
operand also has its easy way to understand the logic. The result is always true
as long as there is a * among the operands and false
when both operands are 0
.
In C#, the &&
operator means AND
, and the ||
operator means OR
.
Let's look at an example of these.
1using System;
2namespace Pluralsight
3{
4 public class LogicalOperators
5 {
6 public static void Main()
7 {
8 bool a = true;
9 bool b = false;
10 Console.WriteLine($"The value of a is {a}, and b is {b}");
11 Console.WriteLine($"The a && b evaluates to {a&&b}");
12 Console.WriteLine($"The a || b evaluates to {a || b}");
13 a = false;
14 b = false;
15 Console.WriteLine($"The value of a is {a}, and b is {b}");
16 Console.WriteLine($"The a && b evaluates to {a && b}");
17 Console.WriteLine($"The a || b evaluates to {a || b}");
18 a = true;
19 b = true;
20 Console.WriteLine($"The value of a is {a}, and b is {b}");
21 Console.WriteLine($"The a && b evaluates to {a && b}");
22 Console.WriteLine($"The a || b evaluates to {a || b}");
23 a = false;
24 b = true;
25 Console.WriteLine($"The value of a is {a}, and b is {b}");
26 Console.WriteLine($"The a && b evaluates to {a && b}");
27 Console.WriteLine($"The a || b evaluates to {a || b}");
28 Console.ReadKey();
29 }
30
31 }
32}
Executing the code gives us the following output.
1The value of a is True, and b is False
2The a && b evaluates to False
3The a || b evaluates to True
4The value of a is False, and b is False
5The a && b evaluates to False
6The a || b evaluates to False
7The value of a is True, and b is True
8The a && b evaluates to True
9The a || b evaluates to True
10The value of a is False, and b is True
11The a && b evaluates to False
12The a || b evaluates to True
The truth table of the NOT
operand:
A | NOT |
---|---|
1 | 0 |
0 | 0 |
In C#, the !
operator means the NOT
operand.
Let's look at a short example as to how it works.
1using System;
2namespace Pluralsight
3{
4 public class LogicalOperators
5 {
6 public static void Main()
7 {
8 bool a = true;
9 Console.WriteLine($"The value of a is {a}");
10 Console.WriteLine($"The !a evaluates to {!a}");
11 a = false;
12 Console.WriteLine($"The value of a is {a}");
13 Console.WriteLine($"The !a evaluates to {!a}");
14 Console.ReadKey();
15 }
16
17 }
18}
Executing the code gives us the following result.
1The value of a is True
2The !a evaluates to False
3The value of a is False
4The !a evaluates to True
You can also combine these expressions to describe more sophisticated logic in your applications.
This type of operator has two types of variables it can work with. One is the integral numeric type, which hold the sbyte, byte, short, ushort, int, uint, long, and ulong subtypes, and the other is the char type. As long as you are using any of these, the operator is going to work.
There are a total of six bitwise operators:
~
- Complement (Flips the bits in a bit stream so the 1
-s become 0
-s and vice versa.)&
- AND (Same logic as in the logical operators section)|
- OR (Same logic as in the logical operators section)^
- Exclusive OR (The bit is flipped if there is a 1
in either operand but not both.)<<
- Left shift (Moves the bits against the left side by the specified amount.)>>
- Right shift (Moves the bits against the right side by the specified amount.)We'll demonstrate these operands with two numbers:
Decimal | Binary |
---|---|
6 | 0000 0110 |
17 | 0001 0001 |
Let's create the demonstration.
1using System;
2namespace Pluralsight
3{
4 public class LogicalOperators
5 {
6 public static void Main()
7 {
8 int a = 6;
9 int b = 17;
10 Console.WriteLine($"The value of a is {a} in binary: {Convert.ToString(a, 2)}, b is {b} in binary: {Convert.ToString(b, 2)}!");
11 Console.WriteLine("Bitwise OR");
12 Console.WriteLine($"The bitwise OR result is {a | b} in binary: { Convert.ToString((a|b), 2)}");
13 Console.WriteLine("Bitwise AND");
14 Console.WriteLine($"The bitwise AND result is {a & b} in binary: { Convert.ToString((a & b), 2)}");
15 Console.WriteLine("Bitwise Complement");
16 Console.WriteLine($"The bitwise AND result is {~a} in binary: { Convert.ToString((~a), 2)}");
17 Console.WriteLine("Bitwise XOR");
18 Console.WriteLine($"The bitwise AND result is {a ^ b} in binary: { Convert.ToString((a ^ b), 2)}");
19 Console.WriteLine("Bitwise <<");
20 Console.WriteLine($"The bitwise AND result is {a << b} in binary: { Convert.ToString((a << b), 2)}");
21 Console.WriteLine("Bitwise >>");
22 Console.WriteLine($"The bitwise AND result is {a >> b} in binary: { Convert.ToString((a >> b), 2)}");
23 Console.ReadKey();
24 }
25
26 }
27}
Executing the code gives us the following result.
1The value of a is 6 in binary: 110, b is 17 in binary: 10001!
2Bitwise OR
3The bitwise OR result is 23 in binary: 10111
4Bitwise AND
5The bitwise AND result is 0 in binary: 0
6Bitwise Complement
7The bitwise AND result is -7 in binary: 11111111111111111111111111111001
8Bitwise XOR
9The bitwise AND result is 23 in binary: 10111
10Bitwise <<
11The bitwise AND result is 786432 in binary: 11000000000000000000
12Bitwise >>
13The bitwise AND result is 0 in binary: 0
These two categories of operators in C# may be the hardest to grasp because they go against the dogma of the decimal number system taught to us in school. Entire careers can be built without utilizing any of these operators based on special circumstances, but it's still good to clarify these concepts in your head. With time, it becomes obvious that behind the scenes of each and every application, these types of manipulations are happening—we just have a pretty good abstraction layer that helps us overcome the binary system.
I hope this has been informative for you and I would like to thank you for reading it!