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 1 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.
  • 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