SoFunction
Updated on 2025-03-06

Introduction and examples of C# displacement

Actually very simple. . .

C# displacement operator:

Move left: <<

Move right:>>

The displacement understanding may be simpler: in fact, it is the conversion of data into binary left and right movements; move right and left to make up 0, move left and right to make up 0, and remove the extra parts behind.

It is also possible to understand displacement using multiplication and division:

Left displacement: equivalent to multiplication

The left shift of 1 bit is equivalent to multiplying 2, the left shift of 2 bits is equivalent to multiplying 4, the left shift of 3 bits is equivalent to multiplying 8, the left shift of 4 bits is equivalent to multiplying 16... Analog

Right displacement: equivalent to

Moving right by 1 bit is equivalent to dividing 2, shift right by 2 bit is equivalent to dividing 4, shift right by 3 bit is equivalent to dividing 8, shift right by 4 bit is equivalent to dividing 16... Analog

Let’s understand the operation of displacement using a question that once answered a netizen.

Title: Shift 89 right by one:

Copy the codeThe code is as follows:

string flag = (89, 2); //This is to convert your 89 into a 2-digit number. .

//flag result: 1011001
//You need to move the right, add 0 on the left, and remove one from the back

int j = Convert.ToInt32("0101100", 2); //Then convert the bin into the decimal number. .

//Result: 44

//The displacement is that simple

This way it is easy to understand displacement operations...