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

    So, are you saying you can't use & in that case?! They do the same except for the fact that && is short-circuited.

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

    I don't know which language you are talking about, but in C#, they have different meaning, bitwise operators and logical operators are not the same. And in C# all logical operators always short-circuit.

    F 1 Reply Last reply
    0
    • L Lost User

      I don't know which language you are talking about, but in C#, they have different meaning, bitwise operators and logical operators are not the same. And in C# all logical operators always short-circuit.

      F Offline
      F Offline
      Fabio V Silva
      wrote on last edited by
      #6

      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.

      L H M 4 Replies Last reply
      0
      • G gumi_r msn com

        Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM. :sigh:

        F Offline
        F Offline
        Fabio V Silva
        wrote on last edited by
        #7

        You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...

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

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

          Read my comments again, I said that the LOGICAL operators (&&, ||) always short-circuit, but bitwise operators never short-circuit. The links you posted say exactly the same. See this example for the difference between & and &&: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]

          F 1 Reply Last reply
          0
          • F Fabio V Silva

            You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...

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

            Fabio V Silva wrote:

            You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...

            I confirm before posting anything, check my reply.

            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.

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

              And before asking me to RTFM, you should go to a nursery that teaches the rudiments of programming. BITWISE operators and LOGICAL operators have totally different meanings even though they may produce the same output in certain situations: Read this link again: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]

              1 Reply Last reply
              0
              • L Lost User

                Read my comments again, I said that the LOGICAL operators (&&, ||) always short-circuit, but bitwise operators never short-circuit. The links you posted say exactly the same. See this example for the difference between & and &&: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]

                F Offline
                F Offline
                Fabio V Silva
                wrote on last edited by
                #11

                Shameel wrote:

                And in C# all logical operators always short-circuit.

                You said all logical operators always short-circuit and that's not true. & and | are logical operators and they don't short-circuit. I never said & can't be a bitwise and but in this case it is a logical and.

                L 1 Reply Last reply
                0
                • F Fabio V Silva

                  Shameel wrote:

                  And in C# all logical operators always short-circuit.

                  You said all logical operators always short-circuit and that's not true. & and | are logical operators and they don't short-circuit. I never said & can't be a bitwise and but in this case it is a logical and.

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

                  I am tired of explaining to you, & is NOT a logical operator, it is a bitwise operator.

                  F 1 Reply Last reply
                  0
                  • L Lost User

                    I am tired of explaining to you, & is NOT a logical operator, it is a bitwise operator.

                    F Offline
                    F Offline
                    Fabio V Silva
                    wrote on last edited by
                    #13

                    So, in this case - if (true & false) - & is not a logical AND?

                    L 1 Reply Last reply
                    0
                    • F Fabio V Silva

                      So, in this case - if (true & false) - & is not a logical AND?

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

                      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.

                      F J 2 Replies Last reply
                      0
                      • G gumi_r msn com

                        Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM. :sigh:

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

                        As I have stated in my reply to Fabio, & and && are for different purpose. & is a bitwise operator and && is a logical operator, they may produce same output in certain situations, but it does not mean that you can use them interchangeably. In C#, logical operators always short-circuit. See this link for an example: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]

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

                          F Offline
                          F Offline
                          Fabio V Silva
                          wrote on last edited by
                          #16

                          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 D 3 Replies 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
                            #17

                            Please read the msdn links that you posted, VB is different and C# is different. VB has two logical operators for AND operation, And does not short-circuit and AndAlso short-circuits. This is a design decision for backward compatibility with VB6. But C# was designed from the ground-up, so the designers were not constrained by the need to be backward compatible, they had more freedom which made them design && to always short-circuit. Read this link which CLEARLY states that & and && are different: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^] If you're still not convinced, then good luck to you, this is my last reply to this post.

                            F 1 Reply Last reply
                            0
                            • L Lost User

                              Please read the msdn links that you posted, VB is different and C# is different. VB has two logical operators for AND operation, And does not short-circuit and AndAlso short-circuits. This is a design decision for backward compatibility with VB6. But C# was designed from the ground-up, so the designers were not constrained by the need to be backward compatible, they had more freedom which made them design && to always short-circuit. Read this link which CLEARLY states that & and && are different: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^] If you're still not convinced, then good luck to you, this is my last reply to this post.

                              F Offline
                              F Offline
                              Fabio V Silva
                              wrote on last edited by
                              #18

                              What you don't seem to realise is that I'm not talking about the operators names but what they do. From MSDN[^]: & - logical AND | - logical OR && - conditional AND || - conditional OR From MSDN[^]: The operation x && y corresponds to the operation x & y except that if x is false, y is not evaluated (because the result of the AND operation is false no matter what the value of y may be). This is known as "short-circuit" evaluation. Which means I was right in my first post to say there was no difference, in that if condition, to use one or the other, the program still runs with no exceptions and as expected. Your only argument so far is that one is called logical and the other bitwise which doesn't change the fact that I was right; call them what you want.

                              L 1 Reply Last reply
                              0
                              • F Fabio V Silva

                                What you don't seem to realise is that I'm not talking about the operators names but what they do. From MSDN[^]: & - logical AND | - logical OR && - conditional AND || - conditional OR From MSDN[^]: The operation x && y corresponds to the operation x & y except that if x is false, y is not evaluated (because the result of the AND operation is false no matter what the value of y may be). This is known as "short-circuit" evaluation. Which means I was right in my first post to say there was no difference, in that if condition, to use one or the other, the program still runs with no exceptions and as expected. Your only argument so far is that one is called logical and the other bitwise which doesn't change the fact that I was right; call them what you want.

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

                                This is true only in case where the operands of & are both bool, but unlike && which takes only bool operands, & can also take int as operands which can lead to unexpected behavior.

                                F 1 Reply Last reply
                                0
                                • L Lost User

                                  This is true only in case where the operands of & are both bool, but unlike && which takes only bool operands, & can also take int as operands which can lead to unexpected behavior.

                                  F Offline
                                  F Offline
                                  Fabio V Silva
                                  wrote on last edited by
                                  #20

                                  And what's the case on the line of code in my first post?

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

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

                                    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]

                                    F Q 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]

                                      F Offline
                                      F Offline
                                      Fabio V Silva
                                      wrote on last edited by
                                      #22

                                      I was just discussing the case that someone said that line was incorrect and downvoted my answer when in reality I just copied the OP's line and changed the = to == which is what was actually stopping it from working as expected. I prefer the double ampersand as well as a standard, the same way I always try to use short circuiting in VB which doesn't seem to be common practice, at least in online samples and articles.

                                      1 Reply Last reply
                                      0
                                      • F Fabio V Silva

                                        And what's the case on the line of code in my first post?

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

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

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

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

                                          In the case you have posted the final result would be the same as if you had used the && operator. There is, however, an important difference between & and &&. Have a look at this example:

                                          ICollection col = null;
                                          if (col != null && col.Count > 0)
                                          // Whatever

                                          This would first check this expression: col != null; like it is false and we are using && operator, it would not even try to check the second operand, becouse the final result will be false. However, if we use just the bitwise & operator:

                                          ICollection col = null;
                                          if (col != null & col.Count > 0)
                                          // Whatever

                                          Since & operator is a bitwise operator it will try to check both operands so, in this case, it would throw a NullReferenceException when trying to evaluate the result of the second operand. That is why we always use the && operator in our boolean expressions, placing each operand in the right place. I guess this is what others have been trying to explain to you in this thread. That said, I would not have downvoted your answer just for this if the rest of the answer is correct, and I think that not being able to make you understand this is not reason enough to tell them to RTFM.

                                          F 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