Bitwise Operators in C#🕶👓

bitwise In c#, Bitwise Operators will work on bits, and these are useful to perform bit by bit operations such as And &(Both), or|(Either),Xor^(Exclusive or, different), Not~(Invert). Most importantly, we can perform bit-level operations on Boolean and integer data. Biwise operators enables you to manipulate data at the basic binary level. Maybe because we are not classically taught math from this perspective or generally we don't process data in 1 or 0 but rather in a more visually distinguishable form, it can be rather confusing for novice programmers to interpret or visualize what they do.Without using circuit diagram, we can show the operation of bitwise operators in c# where 0 is off and 1 is On. Explicitly, bitwise AND takes two bit sequences 1100 and 1010 If both of them are 1, the result is 1 and if any or both of them are 0, the answer is 0. Considering biwise OR, if any of them is 1, then the result is 1. Exclusive OR is similarly except that both of the binary numbers have to be different for it to be 1. NOT reverses all the bits, if it is 0, it reverses to 1 and if 1, it reverses to 0.

Why use Bitwise Operators👌🤷‍♀️

If you don't have much memory, understanding how to control bits gives you more control over the limited resources you have. When you start working close to the hardware, you can use bitwise operations to optimize your code. For instance, you can store eight true, false value (00000001) in 1 byte as:

Boolean isTrue = 00000001

Rather than eight 1 byte boolean:

Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001
Boolean isTrue = 00000001

This automatically helps you save memory and you can use bitwise logic to change the values or you can use very shifting operators to perform multiplications and divisions and hashing algorithms to make it really hard to decode a hash signature move you get two-bit streams and you get the order of them. Its next to impossible to get what the original bit streams were. So it becomes impossible to decode your original message.

Therefore these operators have 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.

A more classified look at the six bitwise operators shows:

  • ~ - 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 can look at this pragmatically with the following code snippet:

using System;
namespace PracticeApp
{
    public class LogicalOperators
    {
        public static void Main()
        {
            int a = 6;
            int b = 17;
            Console.WriteLine($"The value of a is {a} in binary: {Convert.ToString(a, 2)}, b is {b} in binary: {Convert.ToString(b, 2)}!");
            Console.WriteLine("Bitwise OR");
            Console.WriteLine($"The bitwise OR result is {a | b} in binary: { Convert.ToString((a|b), 2)}");
            Console.WriteLine("Bitwise AND");
            Console.WriteLine($"The bitwise AND result is {a & b} in binary: { Convert.ToString((a & b), 2)}");
            Console.WriteLine("Bitwise Complement");
            Console.WriteLine($"The bitwise AND result is {~a} in binary: { Convert.ToString((~a), 2)}");
            Console.WriteLine("Bitwise XOR");
            Console.WriteLine($"The bitwise AND result is {a ^ b} in binary: { Convert.ToString((a ^ b), 2)}");
            Console.WriteLine("Bitwise <<");
            Console.WriteLine($"The bitwise AND result is {a << b} in binary: { Convert.ToString((a << b), 2)}");
            Console.WriteLine("Bitwise >>");
            Console.WriteLine($"The bitwise AND result is {a >> b} in binary: { Convert.ToString((a >> b), 2)}");
            Console.ReadKey();
        }

    }
}

The output of the above code snippet shows: output

Summary🤞🤞

An entire careers can be built without utilizing any of these bitwise 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.

References

https://www.pluralsight.com/guides/logical-and-bitwise-operators-in-c What are Bitwise Operators and why do we use them(Youtube https://www.youtube.com/watch?v=-HADQ0-id_I)

https://subscription.packtpub.com/book/application-development/9781789534313/2/ch02lvl1sec18/swift-operators

0
Subscribe to my newsletter

Read articles from chukwuka chibueze directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

chukwuka chibueze
chukwuka chibueze