开发者

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

开发者 https://www.devze.com 2023-03-29 20:20 出处:网络
I se开发者_StackOverflow社区arched in Google/Stackoverflow.. Didn\'t find something.. it would be interested to know what that means..

I se开发者_StackOverflow社区arched 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);


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消