Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pipe operator

Pipe operator

Scheduled Pinned Locked Moved C#
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    William Winner
    wrote on last edited by
    #1

    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;

    L I P 3 Replies Last reply
    0
    • W William Winner

      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;

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      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)

      W P 2 Replies Last reply
      0
      • W William Winner

        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;

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        G 1 Reply Last reply
        0
        • W William Winner

          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;

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • P Pete OHanlon

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • L Luc Pattyn

              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.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              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

              My blog | My articles | MoXAML PowerToys | Onyx

              I 1 Reply Last reply
              0
              • P Pete OHanlon

                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

                My blog | My articles | MoXAML PowerToys | Onyx

                I Offline
                I Offline
                Ian Shlasko
                wrote on last edited by
                #7

                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)

                L 1 Reply Last reply
                0
                • I Ian Shlasko

                  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)

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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.

                  I 1 Reply Last reply
                  0
                  • I Ian Shlasko

                    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)

                    W Offline
                    W Offline
                    William Winner
                    wrote on last edited by
                    #9

                    Ah...thanks...makes sense!

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      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.

                      I Offline
                      I Offline
                      Ian Shlasko
                      wrote on last edited by
                      #10

                      :laugh:

                      Proud to have finally moved to the A-Ark. Which one are you in?
                      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                      1 Reply Last reply
                      0
                      • I Ian Shlasko

                        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)

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Ian Shlasko wrote:

                        (This only makes sense with flags-type enumerations, of course)

                        That is not necessarily true.

                        I 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Ian Shlasko wrote:

                          (This only makes sense with flags-type enumerations, of course)

                          That is not necessarily true.

                          I Offline
                          I Offline
                          Ian Shlasko
                          wrote on last edited by
                          #12

                          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)

                          A 1 Reply Last reply
                          0
                          • I Ian Shlasko

                            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)

                            A Offline
                            A Offline
                            AspDotNetDev
                            wrote on last edited by
                            #13

                            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").

                            [WikiLeaks Cablegate Cables]

                            P 1 Reply Last reply
                            0
                            • A AspDotNetDev

                              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").

                              [WikiLeaks Cablegate Cables]

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              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 A 2 Replies Last reply
                              0
                              • P PIEBALDconsult

                                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 Offline
                                I Offline
                                Ian Shlasko
                                wrote on last edited by
                                #15

                                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)

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  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.

                                  G Offline
                                  G Offline
                                  GlobX
                                  wrote on last edited by
                                  #16

                                  Haha, sorry Luc, great answer, but I have to tell you how I read this:

                                  Luc Pattyn wrote: |= is to || or | what += is to +

                                  "or equals is to or or or what plus equals is to plus" :laugh:

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    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.

                                    A Offline
                                    A Offline
                                    AspDotNetDev
                                    wrote on last edited by
                                    #17

                                    I didn't know that. Or if I did, I forgot. :thumbsup:

                                    [WikiLeaks Cablegate Cables]

                                    A 1 Reply Last reply
                                    0
                                    • A AspDotNetDev

                                      I didn't know that. Or if I did, I forgot. :thumbsup:

                                      [WikiLeaks Cablegate Cables]

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #18

                                      Testing someting...

                                      [WikiLeaks Cablegate Cables]

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups