Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C# exclusive or `^` usage? [closed]

Can anyone explain this operator with a good example?

I know what this operator is. I mean a real-life example.

like image 397
Navid Rahmani Avatar asked Jun 21 '11 08:06

Navid Rahmani


People also ask

What is meant by C?

noun, plural C's or Cs, c's or cs. the third letter of the English alphabet, a consonant. any spoken sound represented by the letter C or c, as in cat, race, or circle. something having the shape of a C. a written or printed representation of the letter C or c.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C and C++ difference?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.

What is C language with example?

C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.


2 Answers

For example, like this:

var result = a ^ b;

result          a        b
--------------------------------
true            true    false
true            false   true
false           true    true
false           false   false
like image 67
shenhengbin Avatar answered Oct 06 '22 23:10

shenhengbin


It is an implementation of the the logical operation exclusive disjunction

http://en.wikipedia.org/wiki/Exclusive_or

Exclusive disjunction is often used for bitwise operations. Examples:

  • 1 xor 1 = 0
  • 1 xor 0 = 1
  • 0 xor 1 = 1
  • 0 xor 0 = 0
  • 1110 xor 1001 = 0111 (this is equivalent to addition without carry)

As noted above, since exclusive disjunction is identical to addition modulo 2, the bitwise exclusive disjunction of two n-bit strings is identical to the standard vector of addition in the vector space (Z/2Z)^4.

In computer science, exclusive disjunction has several uses:

  • It tells whether two bits are unequal.
  • It is an optional bit-flipper (the deciding input chooses whether to invert the data input).
  • It tells whether there is an odd number of 1 bits ( is true iff an odd number of the variables are true).

(and a whole ton of other uses)

like image 33
Merlyn Morgan-Graham Avatar answered Oct 06 '22 23:10

Merlyn Morgan-Graham