Bitwise operations and bitwise hacks in C#

  

6
Старт на темата

It is hard for me to understand the bitwise operations. Can you please give me some examples of such or maybe some bitwise hacks which can be useful in exams and in programming in C#? Thanks!

4 Отговори
4

5 FIVE of the most used bitwise operations

(see the comments in the code):


1. Showing the most right bit (see line #10):

using System;
 
class BitwiseOperations
{
    static void Main()
    {
        int a = 7;
        Console.WriteLine(Convert.ToString(a, 2).PadLeft(32, '0'));//Seeing the number in bits
 
        int result = a & 1;//using & operator
        Console.WriteLine(Convert.ToString(result, 2).PadLeft(32, '0'));//Seeing the most right bit
        Console.WriteLine(result);
    }
}

2. Showing a bit with defined position (see lines #11 and #14):

using System;
 
class BitwiseOperations
{
    static void Main()
    {
        int a = 444;
        Console.WriteLine("The NUMBER: " + Convert.ToString(a, 2).PadLeft(32, '0'));
 
        int p = 2;
        int aRightp = a >> p;//using shift >> operator
        Console.WriteLine("  The MASK: " + Convert.ToString(aRightp, 2).PadLeft(32, '0'));
 
        int result = aRightp & 1;//using & operator + putting 1
        Console.WriteLine("The RESULT: " + Convert.ToString(result, 2).PadLeft(32, '0'));
        Console.WriteLine(result);
    }
}

3. Changing user-defined bit with 1 (see lines #11 and #14):

using System;
 
class BitwiseOperations
{
    static void Main()
    {
        int a = 2;
        Console.WriteLine("The NUMBER: " + Convert.ToString(a, 2).PadLeft(32, '0'));
 
        int p = 2;
        int mask = 1 << p;//SHIFT operator << on left + putting 1
        Console.WriteLine("  The MASK: " + Convert.ToString(mask, 2).PadLeft(32, '0'));
 
        int result = mask | a;//using | operator on the mask and the number
        Console.WriteLine("The RESULT: " + Convert.ToString(result, 2).PadLeft(32, '0'));
        Console.WriteLine(result);
    }
}

4. Changing user-defined bit with 0 (see lines #11 and #14):

using System;
 
class BitwiseOperations
{
    static void Main()
    {
        int a = 6;
        Console.WriteLine("The NUMBER: " + Convert.ToString(a, 2).PadLeft(32, '0'));
 
        int p = 2;
        int mask = ~(1 << p);//(using ~) + (SHIFT on left and putting 1)
        Console.WriteLine("  The MASK: " + Convert.ToString(mask, 2).PadLeft(32, '0'));
 
        int result = mask & a;//using & operator on the mask and the number
        Console.WriteLine("The RESULT: " + Convert.ToString(result, 2).PadLeft(32, '0'));
        Console.WriteLine(result);
    }
}

5. Changing user-defined bit with the opposite bit (see lines #11 and #14):

using System;
 
class BitwiseOperations
{
    static void Main()
    {
        int a = 2;
        Console.WriteLine("The NUMBER: " + Convert.ToString(a, 2).PadLeft(32, '0'));
 
        int p = 2;
        int mask = 1 << p;
        Console.WriteLine("  The MASK: " + Convert.ToString(mask, 2).PadLeft(32, '0'));
 
        int result = mask ^ a;//using ^ operator on the mask and the number
        Console.WriteLine("The RESULT: " + Convert.ToString(result, 2).PadLeft(32, '0'));
        Console.WriteLine(result);
    }
}

3 useful videos to watch about bitwise operators in C#:

4

First you must know about the bitwise operators:

& AND

| OR

^ XOR

~ NOT

>> SHIFT RIGHT

<< SHIFT LEFT

Here is a table which will exmplain how to use them:

bitwise operators


Then use the table below to see how to replace the bits themselves:

replacing the bits csharp

3

Here is one simple hack to check whether a number is EVEN or ODD:

  • The number is EVEN if it's youngest bit0 is 0.
  • In 0001 0111 - the youngest bit (bit0) is 1 - so it is ODD)

To find it out for sure (and create console application) - we need to use & (AND) operator with 1 which will replace all the other bits except for the youngest one - b0;

If the result of this operation is 0 - the number is EVEN; if it is 1 - it is ODD;

Here is an example to make it clear:

The number 23 - it is ODD:

The binary representation of 23 is 10111 (use Windows Calculator) - as you can see the youngest bit (bit0) - is 1 - so we know that the number is ODD.

In order to write a program we'll need the & operator - and it will look like this:

0001 0111  &
0000 0001   (1 in this case is: 0000 0001)
––––
0000 0001

The code in Visual Studio for C# will look like this:

using System;
 
class Bits
{
    static void Main()
    {
        int number = int.Parse(Console.ReadLine());
        if ((number & 1) == 0)
        {
            Console.WriteLine("The number {0} is EVEN", number);
        }
        else if ((number & 1) != 0) //or ((number & 1) == 1)
        {
            Console.WriteLine("the number {0} is ODD", number);
        }
    }
}
2

Here is a way to print  a number into binary format:

        int number = 23;
        Console.WriteLine("The number in Binary format is: {0}", Convert.ToString(number, 2));

2 represents binary format

The output will be:

The number in Binary format is: 10111

Here is how to find a particualar bit on a particular position - let's say the position "p" (we will be using mask).

Here is an example:

int number = 35;                 // 00000000 00100011
int position = 4;
int mask = 1 << position;          // 00000000 00010000
int result = number & mask;      // 00000000 00010000
If the result == 0 it means that the bit on position 4 is equal to 0
If the result != 0 it means that the bit on position 4 is equal to 1 (! is NOT - DO NOT WRITE result == 1)
 
And at the end: very useful article for understanding the bitwise operations and hacks: https://catonmat.net/low-level-bit-hacks
 
Споделете: