C # operator and, Or, XOR and shift operations

Source: Internet
Author: User

C # operator and, Or, XOR and shift operations

1. Analyze the exclusive or operation (^)

Binary ^ operators are predefined for integer and bool types. For an integer, ^ returns the bitwise XOR of the calculated operand ". For the bool operand, the logic of the ^ calculated operand is "exclusive or". That is to say, the result is true only when only one operand is true.

Numerical calculation example

Three Characteristics of bitwise XOR:
(1) 0 ^ 0 = 0 ^ 1 = 1 0 exclusive or any number = any number
(2) 1 ^ 0 = ^ 1 = 0 1 exclusive or any number-inverse of any number
(3) 1 ^ 1 = 0 0 ^ 0 = 0 any number is different or you = set yourself to 0

Example: 10100001 ^ 00010001 = 10110000

Common usage of bitwise XOR:
(1) Flip certain specific bits
For example, if the logarithm is 10100001 bits and 2nd bits are flipped, bitwise exclusive or operations can be performed on the number and 3rd bits.
10100001 ^ 00000110 = 10100111

(2) Exchange two values without using temporary variables.
For example, you can use the following statement to exchange values of two integers, A = 10100001 and B = 00000110:
A = a ^ B; // A = 10100111
B = B ^ A; // B = 10100001
A = a ^ B; // A = 00000110

(3) In assembly languages, it is often used to set variables to zero:
XOR a,

(4) quickly determine whether two values are equal
Example 1: To determine whether two integers A and B are equal, use the following statement:
Return (a ^ B) = 0)

Example 2: the implementation of the initial functions 6_addr_equal () in Linux is as follows:
Static inline int ipv6_addr_equal (const struct in6_addr * A1, const struct in6_addr * A2)
{
Return (A1-> s6_addr32 [0] = a2-> s6_addr32 [0] &
A1-> s6_addr32 [1] = a2-> s6_addr32 [1] &
A1-> s6_addr32 [2] = a2-> s6_addr32 [2] &
A1-> s6_addr32 [3] = a2-> s6_addr32 [3]);
}

You can use the bitwise variance or quick comparison. The latest implementation has been changed:
Static inline int ipv6_addr_equal (const struct in6_addr * A1, const struct in6_addr * A2)
{
Return (A1-> s6_addr32 [0] ^ A2-> s6_addr32 [0]) |
(A1-> s6_addr32 [1] ^ A2-> s6_addr32 [1]) |
(A1-> s6_addr32 [2] ^ A2-> s6_addr32 [2]) |
(A1-> s6_addr32 [3] ^ A2-> s6_addr32 [3]) = 0 );
}

2 & operators (and)

1 & 0 is 0
0 & 0 is 0
1 & 1 is 1

3 | Operator (OR)

1 & 0 is 1
0 & 0 is 0
1 & 1 is 1

------------------

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

For example
Int I = 7;
Int J = 2;
Console. writeline (I> J); // The output result is 1.

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 result: {1}", shift1.vala, shift3.vala );
Console. writeline ("{0}> 2 result: {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 );
}
}
}

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.

 

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.