Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c# what does the ^ character do? [duplicate]

Tags:

c#

Possible Duplicate:
What are the | and ^ operators used for?

In c# what does the ^ character do?

like image 478
arkina Avatar asked Mar 04 '11 10:03

arkina


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

Is an operator in C?

Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


3 Answers

This is binary XOR operator.

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

like image 183
Oleks Avatar answered Oct 05 '22 05:10

Oleks


The ^ charater, or 'caret' character is a bitwise XOR operator. e.g.

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

http://www.dotnetperls.com/xor

like image 44
George Duckett Avatar answered Oct 05 '22 04:10

George Duckett


Take a look at MSDN ^ Operator (C# Reference)

like image 2
Kalman Speier Avatar answered Oct 05 '22 06:10

Kalman Speier