eMi
eMi

Reputation: 5618

C#: What this sign/character means: ^?

I searched in Google/Stackoverflow.. Didn't find something..

it would be interested to know what that means..

I saw it also in such an Example:

richTextBox.SelectionFont = new Font(richTextBox.SelectionFont, richTextBox.SelectionFont.Style ^ FontStyle.Bold);

Upvotes: 1

Views: 553

Answers (4)

George Duckett
George Duckett

Reputation: 32428

The character is called a carat. It's the bitwise exclusive or (XOR), see here. It reads as return 1 if either operand is 1, but not both.

left ^ right could be composed of ANDs and ORs as:

(left || right) && !(left && right)

Left or right, and not left and right.

In this case it's used to invert one bit (bold) from a value (Style) of an enumeration with the [Flags] attribute. See here. i.e. It's producing a new style that is the same as the selection one, toggling bold.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881563

It's a bitwise exclusive or. In this case, it's used to toggle the bold property of the font style.

For a good explanation of the various bitwise operators, see here.

The way xor works in particular is to set the resultant bit if and only if one of the input bits is set.

So, if we xor the value 1010 with 0010, we get:

  1010
^ 0010
  ----
  1000 (ie, we toggle that third digit).

If we then do the xor again:

  1000
^ 0010
  ----
  1010 (ie, we toggle that third digit back to its original value).

That's why it's a handy way to toggle a specific bit.

Upvotes: 4

Daniel B
Daniel B

Reputation: 2887

Bitwise XOR, see MSDN. For an example / explanation, see here. Bitwise operators are often used in conjunction with "flags", such as whether or not the FontStyle.Bold is applied. Using XOR on it will toggle it back and forth.

Upvotes: 3

BrandonAGr
BrandonAGr

Reputation: 6017

Bitwise Exclusive Or, I would assume the behavior of the code you posted is to toggle the bold attribute of the selected text

Upvotes: 7

Related Questions