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

    Again, what makes you think I don't know the difference between & and &&?! I never said they do the same thing and I do know that && is the most commonly used. That, however, does not make it wrong to use & between two boolean and it does not invalidate the fact that in that case it makes no difference. And if someone says it does then they should RTFM!

    N Offline
    N Offline
    Nagy Vilmos
    wrote on last edited by
    #32

    Fabio V Silva wrote:

    in that case it makes no difference

    Wrong. Take notes if you like:

    A && B

    In this scenario A is evaluated and if it is true then B is evaluated and that is the result, otherwise the result is false and B never gets evaluated.

    A & B

    Both A and B are evaluated and the values are then combined using a Bitwise And operation, the output of which is cast to a Boolean for the result. At any level, these are two very diferent operations.

    Fabio V Silva wrote:

    And if someone says it does then they should RTFM!

    Manual read and understood, I still says it does.


    Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

    G 1 Reply Last reply
    0
    • L Lost User

      Your case is an exception, don't treat it like a rule.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #33

      So are you admitting you were wrong?

      I may or may not be responsible for my own actions

      1 Reply Last reply
      0
      • N Nagy Vilmos

        Fabio V Silva wrote:

        in that case it makes no difference

        Wrong. Take notes if you like:

        A && B

        In this scenario A is evaluated and if it is true then B is evaluated and that is the result, otherwise the result is false and B never gets evaluated.

        A & B

        Both A and B are evaluated and the values are then combined using a Bitwise And operation, the output of which is cast to a Boolean for the result. At any level, these are two very diferent operations.

        Fabio V Silva wrote:

        And if someone says it does then they should RTFM!

        Manual read and understood, I still says it does.


        Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

        G Offline
        G Offline
        gumi_r msn com
        wrote on last edited by
        #34

        Jesus, why do people pretend to know the absolute truth about things without even bothering to do some minimal research. What you are stating is completely wrong. The & operator is an OVERLOADABLE operator. As such, it has predefined behaviours for integral types and boolean types. (int & int) IS NOT THE SAME AS bool & bool. The first performs a logical bitwise AND operation while the latter performs a LOGICAL AND operation. There is no bitwise operation at all if the operator is dealing with two booleans. It is exactly the same as bool && bool except that both terms are evaluated no matter what the first expression evaluates to. If you are not convinced then please read the following MSDN C# reference link: http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^] or better yet: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^]

        _ N N 3 Replies Last reply
        0
        • G gumi_r msn com

          Jesus, why do people pretend to know the absolute truth about things without even bothering to do some minimal research. What you are stating is completely wrong. The & operator is an OVERLOADABLE operator. As such, it has predefined behaviours for integral types and boolean types. (int & int) IS NOT THE SAME AS bool & bool. The first performs a logical bitwise AND operation while the latter performs a LOGICAL AND operation. There is no bitwise operation at all if the operator is dealing with two booleans. It is exactly the same as bool && bool except that both terms are evaluated no matter what the first expression evaluates to. If you are not convinced then please read the following MSDN C# reference link: http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^] or better yet: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^]

          _ Offline
          _ Offline
          _Erik_
          wrote on last edited by
          #35

          Come on, guys. Stop this mess, ok? I don't pretend to know the absolute truth about this but, man, you are wrong or, at least, you might be confusing beginners. The fact that, as you say, "both terms are evaluated no matter what the first expression evaluates to" with the & operator is the key, and it is not a trivial difference. See this example:

          string s = null;
          bool b1 = s != null && s.Length == 0;
          bool b2 = s != null & s.Length == 0;

          You see the operands here are boolean expressions. However, while b1 would be assigned false without any problem, a runtime NullReferenceException would be thrown when trying to assing the value to b2. This is a really important difference. Both operands are not the same and can never be considered as if they were the same. Under some circumstances they can return the same result, yes, but that does not mean that they are exactly the same or that you can use any of them when you use boolean expressions. Can we, please, go on with our lifes now?

          OriginalGriffO 1 Reply Last reply
          0
          • G gumi_r msn com

            Jesus, why do people pretend to know the absolute truth about things without even bothering to do some minimal research. What you are stating is completely wrong. The & operator is an OVERLOADABLE operator. As such, it has predefined behaviours for integral types and boolean types. (int & int) IS NOT THE SAME AS bool & bool. The first performs a logical bitwise AND operation while the latter performs a LOGICAL AND operation. There is no bitwise operation at all if the operator is dealing with two booleans. It is exactly the same as bool && bool except that both terms are evaluated no matter what the first expression evaluates to. If you are not convinced then please read the following MSDN C# reference link: http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^] or better yet: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^]

            N Offline
            N Offline
            Nathan D Cook
            wrote on last edited by
            #36

            I'm just a novice programmer and didn't even realize (or I forgot?) that & was a legal command. I've always just used &&. I'm so confused by the last 30 some posts, I'm going to keep it simple and make sure I never use &.

            OriginalGriffO 1 Reply Last reply
            0
            • N Nathan D Cook

              I'm just a novice programmer and didn't even realize (or I forgot?) that & was a legal command. I've always just used &&. I'm so confused by the last 30 some posts, I'm going to keep it simple and make sure I never use &.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #37

              Wise decision!

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              M 1 Reply Last reply
              0
              • _ _Erik_

                Come on, guys. Stop this mess, ok? I don't pretend to know the absolute truth about this but, man, you are wrong or, at least, you might be confusing beginners. The fact that, as you say, "both terms are evaluated no matter what the first expression evaluates to" with the & operator is the key, and it is not a trivial difference. See this example:

                string s = null;
                bool b1 = s != null && s.Length == 0;
                bool b2 = s != null & s.Length == 0;

                You see the operands here are boolean expressions. However, while b1 would be assigned false without any problem, a runtime NullReferenceException would be thrown when trying to assing the value to b2. This is a really important difference. Both operands are not the same and can never be considered as if they were the same. Under some circumstances they can return the same result, yes, but that does not mean that they are exactly the same or that you can use any of them when you use boolean expressions. Can we, please, go on with our lifes now?

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #38

                Gets my five - nicely argued.

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Wise decision!

                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                  M Offline
                  M Offline
                  Manfred Rudolf Bihy
                  wrote on last edited by
                  #39

                  Agreed! That is of course only if he meant to say: "I'll never use & with boolean operands." :-D

                  OriginalGriffO B 2 Replies Last reply
                  0
                  • A AspDotNetDev

                    This should really mess with your noggin:

                    bool isTrue = true || true && false; // True
                    bool isFalse = true | true && false; // False.

                    Also, given your example, I'd prefer the double ampersand for 2 reasons: 1) for short-circuiting and 2) for standards' sake.

                    [Managing Your JavaScript Library in ASP.NET]

                    Q Offline
                    Q Offline
                    Quirkafleeg
                    wrote on last edited by
                    #40

                    Ahem! The real Hall of Shame is the lack of brackets - pure laziness without realizing a simple misstyping mistiping mistyping can change the behaviour of code. ...most people are not a programming god who knows all precedence rules!

                    1 Reply Last reply
                    0
                    • M Manfred Rudolf Bihy

                      Agreed! That is of course only if he meant to say: "I'll never use & with boolean operands." :-D

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #41

                      That's how I read it. :-D

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      • M Manfred Rudolf Bihy

                        Agreed! That is of course only if he meant to say: "I'll never use & with boolean operands." :-D

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #42

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

                        L 1 Reply Last reply
                        0
                        • G gumi_r msn com

                          Jesus, why do people pretend to know the absolute truth about things without even bothering to do some minimal research. What you are stating is completely wrong. The & operator is an OVERLOADABLE operator. As such, it has predefined behaviours for integral types and boolean types. (int & int) IS NOT THE SAME AS bool & bool. The first performs a logical bitwise AND operation while the latter performs a LOGICAL AND operation. There is no bitwise operation at all if the operator is dealing with two booleans. It is exactly the same as bool && bool except that both terms are evaluated no matter what the first expression evaluates to. If you are not convinced then please read the following MSDN C# reference link: http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^] or better yet: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^]

                          N Offline
                          N Offline
                          Nagy Vilmos
                          wrote on last edited by
                          #43

                          Well f. me sideways and call me Dr Dream. You come and tell me that I don't know what I'm saying and immediately say what I said. Bitwise means EVERYTHING is evaluated and then anded ored noted xored and stuck through the mincer. Binary menas once the result is known it stops. I appologise if using technical terms confussed you but that's it.


                          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

                          1 Reply Last reply
                          0
                          • F Fabio V Silva

                            Again, what makes you think I don't know the difference between & and &&?! I never said they do the same thing and I do know that && is the most commonly used. That, however, does not make it wrong to use & between two boolean and it does not invalidate the fact that in that case it makes no difference. And if someone says it does then they should RTFM!

                            S Offline
                            S Offline
                            sucram
                            wrote on last edited by
                            #44

                            Shut the f... up.

                            Ego non sum semper iustus tamen Ego sum nunquam nefas!

                            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?"

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

                              So if I have this straight, in your example:

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

                              because both arguments are boolean, the '&' is effectively acting just like a '&&' except for being trivially less efficient because it is always doing both of the string compares. I know in this case you were merely quoting previous post using '&', but even if the non-short-circuit behaviour would be useful sometime, I'd avoid it because it just looks wrong to me. We're in a mixed C++/C# environment here, and I have to be on the lookout for misused '&'s in the code as it is. Allowing for false positives is not in the cards here. That said, I think you got a raw deal.

                              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?"

                                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
                                          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