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. Other Discussions
  3. The Weird and The Wonderful
  4. Logic

Logic

Scheduled Pinned Locked Moved The Weird and The Wonderful
comhelpquestion
56 Posts 24 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.
  • F Fabio V Silva

    I just got an answer[^] downvoted in Q&A because I used & instead of && in this line:

    if(UsernameTextBox.Text == "Manager" & PasswordTextBox.Text == "Maintenance")

    I'm still waiting for a response to my "Why?"

    R Offline
    R Offline
    Rob Grainger
    wrote on last edited by
    #46

    And quite rightly so. While for booleans, & can work as a logical operator, in all other cases it is bitwise. For consistency, use a single operatopr to represent logical operators throughout, the C# designers (C really) chose && for this purpose. It may work, but its obfuscated, and should be rejected or corrected by any reasonable code review, regardless of any appeals you make to technical documentation.

    T 1 Reply Last reply
    0
    • R Rob Grainger

      And quite rightly so. While for booleans, & can work as a logical operator, in all other cases it is bitwise. For consistency, use a single operatopr to represent logical operators throughout, the C# designers (C really) chose && for this purpose. It may work, but its obfuscated, and should be rejected or corrected by any reasonable code review, regardless of any appeals you make to technical documentation.

      T Offline
      T Offline
      Timothy Byrd
      wrote on last edited by
      #47

      Rob, I think the problem is that you are assuming human beings are rational/reasonable. Where I am currently, I have to fill out a form and get authorization to fix a simple memory leak. The code base is several million lines of C++, suffering from 20 years of technical debt. Since I already have a reputation for being "too critical about code quality" which causes my input to get knocked down a level or two, I have to bite my tongue a lot. It's a grand learning experience, but I'll be glad when I figure out what the lesson is!

      F 1 Reply Last reply
      0
      • T Timothy Byrd

        Rob, I think the problem is that you are assuming human beings are rational/reasonable. Where I am currently, I have to fill out a form and get authorization to fix a simple memory leak. The code base is several million lines of C++, suffering from 20 years of technical debt. Since I already have a reputation for being "too critical about code quality" which causes my input to get knocked down a level or two, I have to bite my tongue a lot. It's a grand learning experience, but I'll be glad when I figure out what the lesson is!

        F Offline
        F Offline
        Firo Atrum Ventus
        wrote on last edited by
        #48

        The lesson : Never start a fight in the Hall of Shame :laugh:

        1 Reply Last reply
        0
        • F Fabio V Silva

          You're wrong, they don't always short circuit. See here[^] and here[^]. If you're working as C# developer I think you should RTFM.

          H Offline
          H Offline
          hairy_hats
          wrote on last edited by
          #49

          Fabio V Silva wrote:

          If you're working as C# developer I think you should RTFM.

          No need to be impolite.

          1 Reply Last reply
          0
          • F Fabio V Silva

            Again, you're wrong, they are both logical operators in that case but one is short-circuited and the other is not! You have the same think in VB with the And, AndAlso, Or, OrElse operators, they are all handy in different situations.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #50

            'AND' in VB is not equivalent to '&' in C#, but equivalent '&&'. this is applicable for OR also... Only difference is 'AND' is not short circuited where '&&' is short circuited. To achieve short circuited and VB require explicit usage of 'AND ALSO'

            A 1 Reply Last reply
            0
            • L Lost User

              'AND' in VB is not equivalent to '&' in C#, but equivalent '&&'. this is applicable for OR also... Only difference is 'AND' is not short circuited where '&&' is short circuited. To achieve short circuited and VB require explicit usage of 'AND ALSO'

              A Offline
              A Offline
              agolddog
              wrote on last edited by
              #51

              But nobody here can understand why I continue to suggest we move from VB to C#. Of course we want the default behavior to be as inefficient as possible!

              1 Reply Last reply
              0
              • F Fabio V Silva

                Again, you're wrong, they are both logical operators in that case but one is short-circuited and the other is not! You have the same think in VB with the And, AndAlso, Or, OrElse operators, they are all handy in different situations.

                D Offline
                D Offline
                dchrno
                wrote on last edited by
                #52

                In your example it's a waste of CPU cycles not to short circuit using &&. The exception is if you have somehow overloaded the & operator, or have logic in your Text property that needs to be evaluated every time.

                1 Reply Last reply
                0
                • L Lost User

                  It is your mistake that you are using & like a logical operator. It is NOT supposed to be used as a logical operator, we have && for that purpose.

                  J Offline
                  J Offline
                  Jason Christian
                  wrote on last edited by
                  #53

                  http://msdn.microsoft.com/en-us/library/aa691306%28v=vs.71%29.aspx[^] This (from Microsoft) includes & and | in the logical operators. And semantics aside, they can and sometimes should be used as such in C# - for those cases where you want a logical operator without short-circuiting (i.e. one of the operands is a method call with side-effects - which would be a whole nother type of questionable practice, but the language allows it and it has is uses). So in that sense the OP is correct, & and && do the same thing except && short-circuits. Of course, they don't do exactly the same thing, because & can also be used on non-boolean types as a bitwise operator. So (4 & 5) is meaningful, whereas 4 && 5 is not.

                  1 Reply Last reply
                  0
                  • B BobJanova

                    Indeed. & and | on ints (or uints) is very useful.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #54

                    Absolutely, because && and || wouldn't work with ints (and uints).

                    1 Reply Last reply
                    0
                    • F Fabio V Silva

                      I just got an answer[^] downvoted in Q&A because I used & instead of && in this line:

                      if(UsernameTextBox.Text == "Manager" & PasswordTextBox.Text == "Maintenance")

                      I'm still waiting for a response to my "Why?"

                      J Offline
                      J Offline
                      James Lonero
                      wrote on last edited by
                      #55

                      The "answer" person's assumptions doesn't seem to match with what you have in your message. Using & is different than using && and the results could be different, depending on what you are comparing. Since both return either true of false, there will not be a difference in the result. (The only difference is how the result is achieved.) In C and C++, you can "AND items that are not boolean as: int i, j; i = 1; j = 2; if (i and j) --> result is false (bitwise AND: 1 & 2 yields 0 or false). if (i and j) --> result is true (logical AND: 1 && 2 yields non-zero or true). Hope this helps.

                      1 Reply Last reply
                      0
                      • F Fabio V Silva

                        You're wrong, they don't always short circuit. See here[^] and here[^]. If you're working as C# developer I think you should RTFM.

                        M Offline
                        M Offline
                        Mel Padden
                        wrote on last edited by
                        #56

                        Fabio V Silva wrote:

                        If you're working as C# developer I think you should RTFM.

                        You, sir, are a disgrace. Not only are you inaccurate in your assertions, you display the two traits I detest most about *some* people who work in this profession - a lack of respect for those around you, and a blinkered solipsism bordering on egomania. Wise the f**k up son. Nobody's impressed.

                        Smokie, this is not 'Nam. This is bowling. There are rules. www.geticeberg.com http://melpadden.wordpress.com

                        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