在c#中^字符有什么作用?

ark*_*ina 18 c#

可能重复:
什么是| 和^运算符用于?

在c#中^字符有什么作用?

Ale*_*lex 18

这是二元XOR运算符.

二元^运算符是为整数类型和bool预定义的.对于整数类型,^计算其操作数的按位异或.对于bool操作数,^计算其操作数的逻辑异或; 也就是说,当且仅当其中一个操作数为真时,结果才为真.


Geo*_*ett 5

^ charater或'caret'字符是一个按位XOR操作符.例如

using System;

class Program
{
    static void Main()
    {
        // Demonstrate XOR for two integers.
        int a = 5550 ^ 800;
        Console.WriteLine(GetIntBinaryString(5550));
        Console.WriteLine(GetIntBinaryString(800));
        Console.WriteLine(GetIntBinaryString(a));
        Console.WriteLine();

        // Repeat.
        int b = 100 ^ 33;
        Console.WriteLine(GetIntBinaryString(100));
        Console.WriteLine(GetIntBinaryString(33));
        Console.WriteLine(GetIntBinaryString(b));
    }

    /// <summary>
    /// Returns binary representation string.
    /// </summary>
    static string GetIntBinaryString(int n)
    {
        char[] b = new char[32];
        int pos = 31;
        int i = 0;

        while (i < 32)
        {
            if ((n & (1 << i)) != 0)
            {
                b[pos] = '1';
            }
            else
            {
                b[pos] = '0';
            }
            pos--;
            i++;
        }
        return new string(b);
    }
}

^^^ Output of the program ^^^

00000000000000000001010110101110
00000000000000000000001100100000
00000000000000000001011010001110

00000000000000000000000001100100
00000000000000000000000000100001
00000000000000000000000001000101
Run Code Online (Sandbox Code Playgroud)

http://www.dotnetperls.com/xor