by

Beginner’s Guide to Bitwise Operators in C#

If you’ve ever searched Google for operators in C# (or just about any other language) you’ve undoubtedly encountered the bitwise operators, but what the heck are they and what can they do for us?

According to a page I found on TutorialsPoint:

The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:

Operator                             Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. (~A ) = 61, which is 1100 0011 in 2’s complement due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15, which is 0000 1111

In this blog post I am going to cover bit shift operators and give an example of how they can be used to reduce memory complexity.

As you may know, a boolean has one of two values (true or false). The value of a boolean, therefor, is binary and can also be interpreted as a number (0 for false, and 1 for true). As you may also know, many languages store boolean values as bytes. This means that instead of just storing a 0 or 1 they will actually store an entire byte of memory for each boolean (00000000 for false, and 00000001 for true). That looks like 7 bits of wasted space to me. So, how can we eliminate that wasted space and store booleans as bits?

Instead of storing a single boolean as one byte, let’s use a byte to store 8 booleans! But how do we access the byte and get the value of each individual bit? The answer is to use the following expression:

(n >> p) & ((1 << m) – 1)

In this expression, n is the value of the byte where the booleans are stored, p is the (zero-indexed) number of bits that the bit we want to grab is offset from the right, and m is the number of bits we want to access at once.

If our byte is set to the value 254 or 11111110 in binary, and we want to access the first bit from the right the expression would look like this:

(254 >> 0) & ((1 << 1) – 1)

If our byte is set to the value 253 or 11111101 in binary, and we want to access the second bit from the right the expression would look like this:

(253 >> 1) & ((1 << 1) – 1)

The following code will create a byte of data, set to the value 255 (11111111 in binary), and then access the first bit from the right. It will compare the resulting integer to 0 and print True or False to the console window, False for 0 and True for 1.

The Code:

class Program
{
    static void Main(string[] args)
    {
        byte b = 255;
        if (((b >> 0) & ((1 << 1) - 1)) == 0)
        {
            Console.WriteLine("False");
        } else
        {
            Console.WriteLine("True");
        }
        Console.ReadLine();
    }
}

Write a Comment

Comment