C # shift operation (left shift and right shift)

Source: Internet
Author: User

There are too many cows in the garden, and there are almost some typesArticleSome people may have written things that the scalpers have written. Even if they don't, it seems that they don't have that skill.

Today, I wrote a very interesting thing-C # shift operation (left shift and right shift ).

C # uses the <(left shift) and> (right shift) operators to perform the shift operation.

Shift left (<)

Move the first operand to the left and the number of digits specified by the second operand.
The Left shift is equivalent to multiplication. The left shift is equivalent to multiplication 2; the left shift is equivalent to multiplication 4; The Left shift is equivalent to multiplication 8.

X <1 = x * 2
X <2 = x * 4
X <3 = x * 8
X <4 = x * 16

Similarly, the right shift is the opposite:

Shift right (>)
Move the first operand to the right to the number of digits specified by the second operand, and fill in 0 for the null position.

The right shift is equivalent to the entire division. The right shift is equivalent to dividing by 2; the right shift is equivalent to dividing by 4; The right shift is equivalent to dividing by 8.

X> 1 = X/2
X> 2 = x/4
X> 3 = X/8
X> 4 = x/16

When the C # shift operator is declared to be overloaded, the type of the first operand must always contain the class or structure declared by the operator, and the type of the second operand must always be int, for example:

    Class  Program
{
Static Void Main ( String [] ARGs)
{
Shiftclass shift1 = New Shiftclass ( 5 , 10 );
Shiftclass shift2 = Shift1 < 2 ;
Shiftclass shift3 = Shift1 > 2 ;

Console. writeline ( " {0} <2 result: {1} " , Shift1.vala, shift2.vala );
Console. writeline ( " {0} <2 result: {1} " , Shift1.valb, shift2.valb );
Console. writeline ( " {0 }>> 2: {1} " , Shift1.vala, shift3.vala );
Console. writeline ( " {0 }>> 2: {1} " , Shift1.valb, shift3.valb );

Console. Readline ();
}

Public Class Shiftclass
{
Public Int Vala;
Public Int Valb;

Public Shiftclass ( Int Vala, Int Valb)
{
This . Vala = Vala;
This . Valb = Valb;
}

Public Static Shiftclass Operator < (Shiftclass shift, Int Count)
{
Int A = Shift. Vala < Count;
Int B = Shift. valb < Count;
Return New Shiftclass (A, B );
}

Public Static Shiftclass Operator > (Shiftclass shift, Int Count)
{
Int A = Shift. Vala > Count;
Int B = Shift. valb > Count;
Return New Shiftclass (A, B );
}

}
}

 

The output result of the above expression is:

 

 

Because the displacement is faster than the multiplication and division speed, the efficiency requirement is high, and the multiplication and division operator that satisfies the power of 2 can be carried out by means of displacement.

Interesting, right?

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.