Pipe operator
-
Just curious, but I just saw this code and I was wondering what the difference was. The code I saw was (basically):
FontStyle style = someFont.Style;
style |= FontStyle.Italic;How is that any different than:
FontStyle style = someFont.Style;
style = FontStyle.Italic; -
Just curious, but I just saw this code and I was wondering what the difference was. The code I saw was (basically):
FontStyle style = someFont.Style;
style |= FontStyle.Italic;How is that any different than:
FontStyle style = someFont.Style;
style = FontStyle.Italic;The pipe operator (|) is a binary OR... So:
style |= FontStyle.Italic
...is equivalent to...
style = style | FontStyle.Italic
If style is initially zero, this is the same as a straight assignment (0 | x == x), but if you already have an existing value in there (Maybe 'Bold' is another value), the |= operator would make it Italic AND Bold, while an assignment would replace Bold with Italic. (This only makes sense with flags-type enumerations, of course)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Just curious, but I just saw this code and I was wondering what the difference was. The code I saw was (basically):
FontStyle style = someFont.Style;
style |= FontStyle.Italic;How is that any different than:
FontStyle style = someFont.Style;
style = FontStyle.Italic;In C/C++/Java/C#
|=
is to||
or|
what+=
is to+
. So it is a bit-wise or a logical assign-OR, depending on the operands' types. :)Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
Just curious, but I just saw this code and I was wondering what the difference was. The code I saw was (basically):
FontStyle style = someFont.Style;
style |= FontStyle.Italic;How is that any different than:
FontStyle style = someFont.Style;
style = FontStyle.Italic;William Winner wrote:
FontStyle style = someFont.Style; style = FontStyle.Italic;
This is not equivalent to the first version, which effectively could be written as
FontStyle style = someFont.Style | FontStyle.Italic;
This means that you are doing a logicalbitwise OR on the style, whereas your example here overwrites the style. [Edit]Thanks for pointing out the error in this statement goes to Luc.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, January 3, 2011 3:26 PM
-
William Winner wrote:
FontStyle style = someFont.Style; style = FontStyle.Italic;
This is not equivalent to the first version, which effectively could be written as
FontStyle style = someFont.Style | FontStyle.Italic;
This means that you are doing a logicalbitwise OR on the style, whereas your example here overwrites the style. [Edit]Thanks for pointing out the error in this statement goes to Luc.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, January 3, 2011 3:26 PM
as FontStyle is an enum, hence a numeric, it would be a bit-wise OR, not a logical one. :)
Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
as FontStyle is an enum, hence a numeric, it would be a bit-wise OR, not a logical one. :)
Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
Doh. I completely forgot it was an enum. Slaps side of head.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Doh. I completely forgot it was an enum. Slaps side of head.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Don't feel bad... Luc did too... He had fixed it by the time I clicked Reply on his post to point it out :)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Don't feel bad... Luc did too... He had fixed it by the time I clicked Reply on his post to point it out :)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)With two small differences: I noticed the mistake and fixed it; and I didn't slap Pete's head. :)
Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
The pipe operator (|) is a binary OR... So:
style |= FontStyle.Italic
...is equivalent to...
style = style | FontStyle.Italic
If style is initially zero, this is the same as a straight assignment (0 | x == x), but if you already have an existing value in there (Maybe 'Bold' is another value), the |= operator would make it Italic AND Bold, while an assignment would replace Bold with Italic. (This only makes sense with flags-type enumerations, of course)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ah...thanks...makes sense!
-
With two small differences: I noticed the mistake and fixed it; and I didn't slap Pete's head. :)
Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
:laugh:
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
The pipe operator (|) is a binary OR... So:
style |= FontStyle.Italic
...is equivalent to...
style = style | FontStyle.Italic
If style is initially zero, this is the same as a straight assignment (0 | x == x), but if you already have an existing value in there (Maybe 'Bold' is another value), the |= operator would make it Italic AND Bold, while an assignment would replace Bold with Italic. (This only makes sense with flags-type enumerations, of course)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian Shlasko wrote:
(This only makes sense with flags-type enumerations, of course)
That is not necessarily true.
-
Ian Shlasko wrote:
(This only makes sense with flags-type enumerations, of course)
That is not necessarily true.
Well if it's a regular enumeration (Consecutive integers), you're generally not going to be adding/removing values via bitwise operations... Sure, there could be exceptions, but I can't think of any off-hand.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Well if it's a regular enumeration (Consecutive integers), you're generally not going to be adding/removing values via bitwise operations... Sure, there could be exceptions, but I can't think of any off-hand.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)That does make sense, depending on your meaning. If you mean this:
public enum MyEnum
{
x = 0,
a = 1,
b = 2,
c = 4,
d = 8
}Then yeah, it works perfectly fine. If you meant this:
[Flags]
public enum MyEnum
{
x = 0,
a = 1,
b = 2,
c = 4,
d = 8
}The "Flags" attribute is not actually necessary for the bitwise operations to be successful. It just adds intellisense and changes the behavior of ToString (e.g., it may output "a, d" rather than "9").
-
That does make sense, depending on your meaning. If you mean this:
public enum MyEnum
{
x = 0,
a = 1,
b = 2,
c = 4,
d = 8
}Then yeah, it works perfectly fine. If you meant this:
[Flags]
public enum MyEnum
{
x = 0,
a = 1,
b = 2,
c = 4,
d = 8
}The "Flags" attribute is not actually necessary for the bitwise operations to be successful. It just adds intellisense and changes the behavior of ToString (e.g., it may output "a, d" rather than "9").
Correct. And there are also cases like:
public enum Side
{
None = 0 ,
Left = 1 ,
Right = 2 ,
Both = 3
}where all the bases are covered and you gain nothing by using Flags.
-
Correct. And there are also cases like:
public enum Side
{
None = 0 ,
Left = 1 ,
Right = 2 ,
Both = 3
}where all the bases are covered and you gain nothing by using Flags.
I know... I meant "flags-type" as a way of describing them (Wasn't sure if he was familiar with the term "bitmask")... The attribute is just gravy.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
In C/C++/Java/C#
|=
is to||
or|
what+=
is to+
. So it is a bit-wise or a logical assign-OR, depending on the operands' types. :)Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
Correct. And there are also cases like:
public enum Side
{
None = 0 ,
Left = 1 ,
Right = 2 ,
Both = 3
}where all the bases are covered and you gain nothing by using Flags.
I didn't know that. Or if I did, I forgot. :thumbsup:
-
I didn't know that. Or if I did, I forgot. :thumbsup:
Testing someting...