Bit Processing in C#

By | March 5, 2015

As a .NET programmer, how do you check if a bit is set within a byte? How do you set, unset or toggle a bit? How do you get the binary string representation of a byte? Well for most of those questions, the answer is to use the same bit-twiddling methods that C/C++ programmers are likely familiar with. Thankfully C# supports bitwise operators so we can leverage these existing methods. Towards the end of this article these methods are summed up into a C# class of Extension Methods.

1. Checking if a bit is set at a particular position.

We use the expression (myByte & (1 << position)) != 0 to check if a bit is set. This works by using the Left Shift operator (<<) to take the value of 1 whose binary expression is suprisingly (heavy sarcasm) 00000001 to shift the bit to the index (0-7) which we want to check.

Applying the Left Shift operator on the value of 1 looks like this:

Performing a bitwise AND (&) of the resulting value and the byte will mask out all of the bits we do not care about and unveil to us only the value which we want to check for nonzero. Recall that with bitwise AND both bits must be 1 for the result to be 1, otherwise the result of the operation is 0. If the bit is unset at the specified position then the result of applying the mask will yield a value of zero, otherwise we will get a nonzero value. Hence, that is why we are checking to see if the value is nonzero.

An example:

2. Setting a bit at a particular position.

To set a bit we use the expression myByte | (1 << position). The Left Shift operation on the value of 1 has the same effect as before and this time we perform a bitwise OR (|) on the byte in question and the mask. Remember that for a bitwise OR operation between two bits – If one or both of the two bits is 1 then the result is 1, otherwise the result is 0. Therefore this will leave the other bits in their current state and set the bit at the specified position (if it is not already set).

An example:

3. Unsetting a bit at a particular position.

To unset a bit we use the expression myByte & ~(1 << position). At this point, you are probably noticing a pattern with the Left Shift operation being performed on the value of 1 – In fact every bit twiddling method in this article relies on it. We perform a bitwise NOT (~) on the resulting value from applying the Left Shift operation. The bitwise NOT inverts the bits – Any digit that is a 1 becomes a 0 and any digit that is a 0 becomes a 1.

The result of applying the bitwise NOT (negation):

Now we perform a bitwise AND which will have the effect of leaving all bits in their current setting except the bit at the position we have specified (since 1 x 0 = 0 and 1 x 1 = 1). The bit at the specified position will be bitwise ANDed with a 0 which will always result in a value of 0 at that position.

An example:

4. Toggling a bit at a particular position.

To a toggle a bit at a particular index we use the expression myByte ^ (1 << position). Now the bitwise XOR (Exclusive OR) operation has the effect of resulting in a value of 1 whenever the two bits are opposite in value, otherwise if the values are the same then the result is 0. Applying the XOR operation on the mask and the byte, preserves the bits at the positions we do not care about – As the set bits will be XORed against unset bits which results in a set bit and the unset bits will be XORed against an unset bit which results in an unset bit. Now what is more interesting is what occurs at the bit position we want to toggle. For a bit which is set, it will be XORed against a set bit resulting in an unset bit! For a bit which is unset, it will be XORed against a set bit which results in a set bit! Pretty cool, huh?

Let’s see an example:

5. Getting the binary string representation of a byte in C#.

So this method utilizes Convert.ToString(Byte, Int32) which takes a byte and converts it to a string in the specified base. In this case, we want the binary (base 2) representation. Done? Not quite, the string will not have leading zeroes so the most significant bits (MSBs) will be excluded when zero. I personally do not prefer this so I use String.PadLeft(Int32, Char) to pad the string to the left with zeroes until the binary string is 8 characters in length. The resulting C# code is this:

Byte type Extension Methods Class

Below is a C# class of Byte Extension Methods which encompass the techniques just discussed.

Let’s see these Extension Methods in action with the following Console Application:

This produces the following output:

Byte Extension Methods in Action

The Extension Methods class and Console Application sample are available to download here.

Leave a Reply

Your email address will not be published. Required fields are marked *