| by Arround The Web | No comments

C# Bitwise Left Shift (<<) Operator

In C# programming, we can use the Bitwise operators to perform the bit-level operations. The bit level calculation is done in the CPU when we perform arithmetic operations like addition, subtraction, multiplication, or division. We can also do Bitwise operations in C# programming and the process is simple. The Bitwise operators perform an operation on the given value. The number of bits to shift is also specified. There are six Bitwise operators in C# language, two of them are shift operators.

Our topic of discussion is about the shift operators. The Bitwise left shift operator (<<) will be explained in detail. The shift operator, as the name suggests, shifts the bit from the specified position to the left or to the right. The value is moved to the left by the specified number of bits while using the bitwise left shift operator (<<). The left shift operators take the input only in int (integer), uint (unsigned integer), long (long integer), and ulong (unsigned long integer). When the left operand belongs to a different type, it is converted to the integer type. The data type of the resultant value retains a limit of 32 bits; the output cannot be larger than that. Bitwise operators make the code efficient and faster. Moreover, they offer more accuracy and precision.

Syntax:

Operand_1 << Operand_2

The first “Operand_1” is the value that is shifted from the left to the number of shifts that the “Operand_2” contains. The symbol << left shifts the “Operand_1”.

Example 1:

The Bitwise left shift operator (<<) is applied to the integer values in this program.

using System;

class Program_1
{
    static void Main() {
        int Value_0 = 34;
        int Value_1 = 65;
        int res= Value_0 << Value_1;
        Console.Write("The left shift is  ");
        Console.Write(res);
    }
}

The first statement contains the declaration and initialization of two integer-type variables. The first variable is “Value_0” and the second variable is “Value_1”. The values that are stored in them are “34” and “65”. We left shift the value 34 using the left shift operator (<<). Then, we declare another variable that has an integer data type to save the result. Here, we utilize the left shift operator (<<) as Value_0 << Value_1. This operator left shifts the left operand’s left value by the given value in the second operand. The “res” stores the output of the shift operator. After this, we call the Console.Write() method to print text “The left shift is” and the resultant value that is stored in “res” on the terminal.

Example 2:

Let’s use the left shift operator on the unsigned integer values and see how they produce the output.

using System;

class Program_2
{
    static void Main() {
        uint Val_0 = 4435;
        int  Val_1 = 64;
        uint result= Val_0 << Val_1;
        Console.Write("The left shift is ");
        Console.Write(result);
    }
}

Here, we apply the left shift operator on the unsigned integer type value. One thing you must take care of is that the second operand must be an integer type value because the compiler only takes an integer value to shift.

After calling the static void Main() function, we declare two variables – one of which is an unsigned integer value “Val_0” and the other is an integer value “Val_1”. Then, we define another unsigned integer variable which is “result” to keep the resultant value after left shifting the unsigned integer. We cannot store the result in an integer type variable because after left shifting, the result is an unsigned value. The “Val_0 << Val_1” statement left shifts the left operand that is an unsigned integer value. It produces an unsigned integer value. In the end, show the result on the output screen with the text “The left shift is” using the Console.Write() method:

Example 3:

In this instance, we will talk about the different methods of using the Bitwise left shift operator (<<) on long integer values.

using System;

class Program_3
{
    static void Main() {
        long number_0 = 45;
        long number_1 = 5;
     
        Console.Write("The left shift of long is ");
        Console.WriteLine( number_0 << 3);
        Console.Write("The left shift of long is ");
        Console.Write( number_0 << Convert.ToInt16(number_1));
    }
}

The initialization of two long integer type variables, “number_0” and “number_1”, is done in the first statement. Invoke the Console.Write() function to represent the message “The left shift of long is” and the result on the terminal. Here, we apply the left shift operator (<<) in such a way that we place the first operand as the first variable and the second operand as an integer value. The compiler left shifts the first operand which is “number_0” by 3 and displays the result. In the next statement, print another message on the screen by employing the Console.Write() method. Here, we utilize the first variable, “number_0”, as the first operand and the second variable, “number_1”, as the second operand. The second operand must be an integer type value. We typecast the second variable “number_1” to the integer type using the Convert.ToInt16() function. Then, display the outcome on the console:

Example 4:

This code shows how we can assign the values to an integer after performing the left shift on the unsigned long operator.

using System;

class Program_4
{
    static void Main() {
        ulong number_0 = 445;
     
        Console.Write("The left shift of ulong is ");
        Console.WriteLine( number_0 << 8);
        Console.Write("The left shift of ulong is ");
        Console.WriteLine( number_0 << 16);
        Console.Write("The left shift of ulong is ");
        Console.WriteLine( number_0 << 32);
       
    }
}

First, declare one unsigned long integer type variable which is “number_0”. Next, show the text “The left shift of ulong is” on the terminal by calling the Console.Write() method. We will find the left shift of the “number_0” by an integer value of 8 and we don’t need to store the result anywhere. The Console.WriteLine() function prints the result on the console. Repeat this process twice and change the values of the second operand. By doing this, we can find the left shift of a long unsigned integer type value. But if we want to save the resultant value in a variable, we should keep in the thoughts that the result is the same type as the first operand. The only difference between the Console.Write() and Console.WriteLine() is that the second function prints the outcome and sends the cursor to the next line while the first function only prints the outcome and the cursor blinks on the same line even after displaying the output.

Conclusion

We explored the Bitwise operators in C#, their types, and functionalities. The left shift (<<) operator is applied to shift the number or value by the definite number of bits to the left. The Bitwise operators improve the efficiency of the code and it does not burden the system since they are lightweight operators. Our CPU (computer processing unit) works on the Bitwise level whenever we perform any arithmetic operations. In a nutshell, the Bitwise operators are important in programming and C# supports all the Bitwise operators in which the left shift operator (<<) is one of them.

Share Button

Source: linuxhint.com

Leave a Reply