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. The Lounge
  3. Programming Lesson of the Day

Programming Lesson of the Day

Scheduled Pinned Locked Moved The Lounge
c++question
44 Posts 21 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.
  • D Offline
    D Offline
    den2k88
    wrote on last edited by
    #1

    X & Y

    is not the same as

    X && Y

    Twenty minutes wasted on a condition who refused to yield the correct result.

    CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

    OriginalGriffO T P K G 6 Replies Last reply
    0
    • D den2k88

      X & Y

      is not the same as

      X && Y

      Twenty minutes wasted on a condition who refused to yield the correct result.

      CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

      OriginalGriffO Online
      OriginalGriffO Online
      OriginalGriff
      wrote on last edited by
      #2

      Programming Lesson of the Day #2: If you'd used C# it wouldn't have compiled ... :laugh:

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "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

      Richard DeemingR D K S 4 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Programming Lesson of the Day #2: If you'd used C# it wouldn't have compiled ... :laugh:

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Programming Lesson of the Day #3: Check your assumptions before posting them as a lesson! ;P

        bool x = true;
        bool y = false;
        (x & y).Dump(); // Output: False

        It's not commonly used, but for booleans, & is the non-short-circuiting version of &&. It's the C# equivalent of VB.NET's And, whereas && equates to AndAlso.

        & Operator (C# Reference)[^]:

        Binary & operators are predefined for the integral types and bool. ... For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true. The & operator evaluates both operators regardless of the first one's value.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        OriginalGriffO K S 3 Replies Last reply
        0
        • Richard DeemingR Richard Deeming

          Programming Lesson of the Day #3: Check your assumptions before posting them as a lesson! ;P

          bool x = true;
          bool y = false;
          (x & y).Dump(); // Output: False

          It's not commonly used, but for booleans, & is the non-short-circuiting version of &&. It's the C# equivalent of VB.NET's And, whereas && equates to AndAlso.

          & Operator (C# Reference)[^]:

          Binary & operators are predefined for the integral types and bool. ... For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true. The & operator evaluates both operators regardless of the first one's value.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          OriginalGriffO Online
          OriginalGriffO Online
          OriginalGriff
          wrote on last edited by
          #4

          And I've learned something today! Thank you - I didn't know that. :thumbsup:

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "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 J L 3 Replies Last reply
          0
          • OriginalGriffO OriginalGriff

            Programming Lesson of the Day #2: If you'd used C# it wouldn't have compiled ... :laugh:

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            D Offline
            D Offline
            den2k88
            wrote on last edited by
            #5

            I needed the binary AND (was checking a flag register from a custom device) so it would have been a real issue if it did not compile ;P

            CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

            1 Reply Last reply
            0
            • D den2k88

              X & Y

              is not the same as

              X && Y

              Twenty minutes wasted on a condition who refused to yield the correct result.

              CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #6

              Details! Details!

              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                And I've learned something today! Thank you - I didn't know that. :thumbsup:

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                M Offline
                M Offline
                MacSpudster
                wrote on last edited by
                #7

                All of @OriginalGriff 's Today points should be assigned to @RichardDeeming... ;P

                Richard DeemingR 1 Reply Last reply
                0
                • M MacSpudster

                  All of @OriginalGriff 's Today points should be assigned to @RichardDeeming... ;P

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  Why limit it to today? :-\


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  J 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Programming Lesson of the Day #2: If you'd used C# it wouldn't have compiled ... :laugh:

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #9

                    Depending on the correct implementation of the "&" and "&&" operators in a fat class it shouldnt be the same :rolleyes:

                    Press F1 for help or google it. Greetings from Germany

                    D 1 Reply Last reply
                    0
                    • D den2k88

                      X & Y

                      is not the same as

                      X && Y

                      Twenty minutes wasted on a condition who refused to yield the correct result.

                      CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

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

                      den2k88 wrote:

                      refused to yield the correct expected result.

                      FTFY

                      D 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        And I've learned something today! Thank you - I didn't know that. :thumbsup:

                        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                        J Offline
                        J Offline
                        Jeremy Falcon
                        wrote on last edited by
                        #11

                        Just to expand on Richard's point... I'm more of a C guy than C#, but like with C/C++ I can only assume that in this case, the compiler is doing a binary operation of AND, and 1 (or any non-zero) AND 0 is always going to be zero, which is expressed as false in this case. It's the difference between a bitwise operator and a logical one.

                        Jeremy Falcon

                        OriginalGriffO D 2 Replies Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          Why limit it to today? :-\


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          J Offline
                          J Offline
                          Jeremy Falcon
                          wrote on last edited by
                          #12

                          Because we don't want him to stop posting his thoughts of the day... we need this... our precious. :~

                          Jeremy Falcon

                          1 Reply Last reply
                          0
                          • J Jeremy Falcon

                            Just to expand on Richard's point... I'm more of a C guy than C#, but like with C/C++ I can only assume that in this case, the compiler is doing a binary operation of AND, and 1 (or any non-zero) AND 0 is always going to be zero, which is expressed as false in this case. It's the difference between a bitwise operator and a logical one.

                            Jeremy Falcon

                            OriginalGriffO Online
                            OriginalGriffO Online
                            OriginalGriff
                            wrote on last edited by
                            #13

                            That's why C# didn't adopt the C convention that any non-zero value is true: you can't treat an integer as a boolean directly. Hence why I assumed that the compiler would complain, it does with

                            if (myInt = 6)
                            ...

                            because an integer isn't a boolean value

                            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                            "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

                            J N 2 Replies Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              That's why C# didn't adopt the C convention that any non-zero value is true: you can't treat an integer as a boolean directly. Hence why I assumed that the compiler would complain, it does with

                              if (myInt = 6)
                              ...

                              because an integer isn't a boolean value

                              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                              J Offline
                              J Offline
                              Jeremy Falcon
                              wrote on last edited by
                              #14

                              Oh I hear ya man. I reckon the difference being the compiler knows that's an assignment operator. My understanding of the way C# and Java does things is to create an object where needed from literals / constants / expressions. So, it would be more like this...

                              "Hello, world!".ToUpper()

                              ...just instead of a string it's a false... which gets treated like a boolean.

                              Jeremy Falcon

                              I J 2 Replies Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                That's why C# didn't adopt the C convention that any non-zero value is true: you can't treat an integer as a boolean directly. Hence why I assumed that the compiler would complain, it does with

                                if (myInt = 6)
                                ...

                                because an integer isn't a boolean value

                                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                N Offline
                                N Offline
                                Nelek
                                wrote on last edited by
                                #15

                                the integer is not a boolean, but the "success" of the value assignment can be taken as a bool I have had problems with that several times in my career. That's why I got used to put constants in first place. if (6 = myInt) is going to throw an error in many more cases as the other possibility.

                                M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                1 Reply Last reply
                                0
                                • J Jeremy Falcon

                                  Oh I hear ya man. I reckon the difference being the compiler knows that's an assignment operator. My understanding of the way C# and Java does things is to create an object where needed from literals / constants / expressions. So, it would be more like this...

                                  "Hello, world!".ToUpper()

                                  ...just instead of a string it's a false... which gets treated like a boolean.

                                  Jeremy Falcon

                                  I Offline
                                  I Offline
                                  irneb
                                  wrote on last edited by
                                  #16

                                  Jeremy Falcon wrote:

                                  I reckon the difference being the compiler knows that's an assignment operator.

                                  Nope, it's because both C# and Java are more type safe (strong typed) than C is. In this case they define a boolean type, C just interprets an integer to have similar "meaning" to a boolean. And the if statement (in C#/Java) requires a boolean input argument. It's the same sort of situation as sending a string into a function which expected a float - compiler error - wrong type. This is an example of where C uses a weak typing system. It generally just uses the raw data as if it's in the expected type. Effectively turning it into a raw type-cast. The more strongly typed languages disallow most of these, probably because they tend to be the reason behind lots of bugs (if not most).

                                  OriginalGriffO D J 3 Replies Last reply
                                  0
                                  • D den2k88

                                    X & Y

                                    is not the same as

                                    X && Y

                                    Twenty minutes wasted on a condition who refused to yield the correct result.

                                    CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

                                    K Offline
                                    K Offline
                                    Kirill Illenseer
                                    wrote on last edited by
                                    #17

                                    Actually, the result is the same. The side-effects aren't. At least in C#. In C, with it's enforced type-unsafety, the result can be extremey weird and the compiler won't do jack to even warn you.

                                    D 1 Reply Last reply
                                    0
                                    • I irneb

                                      Jeremy Falcon wrote:

                                      I reckon the difference being the compiler knows that's an assignment operator.

                                      Nope, it's because both C# and Java are more type safe (strong typed) than C is. In this case they define a boolean type, C just interprets an integer to have similar "meaning" to a boolean. And the if statement (in C#/Java) requires a boolean input argument. It's the same sort of situation as sending a string into a function which expected a float - compiler error - wrong type. This is an example of where C uses a weak typing system. It generally just uses the raw data as if it's in the expected type. Effectively turning it into a raw type-cast. The more strongly typed languages disallow most of these, probably because they tend to be the reason behind lots of bugs (if not most).

                                      OriginalGriffO Online
                                      OriginalGriffO Online
                                      OriginalGriff
                                      wrote on last edited by
                                      #18

                                      :thumbsup:

                                      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                      "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
                                      • P PIEBALDconsult

                                        den2k88 wrote:

                                        refused to yield the correct expected result.

                                        FTFY

                                        D Offline
                                        D Offline
                                        den2k88
                                        wrote on last edited by
                                        #19

                                        "If I put in wrong data, will the result be correct?"

                                        CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

                                        P 1 Reply Last reply
                                        0
                                        • K Kirill Illenseer

                                          Actually, the result is the same. The side-effects aren't. At least in C#. In C, with it's enforced type-unsafety, the result can be extremey weird and the compiler won't do jack to even warn you.

                                          D Offline
                                          D Offline
                                          den2k88
                                          wrote on last edited by
                                          #20

                                          No. Example 2 & 4 is 0 2 && is 1 (true) In that case I needed the first form as I was checking for a flag in a flag register, but mistakenly used the second form due to, well, being the most common (althought not so much in my field, which requires the management of a lot of driverless hardware).

                                          CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"

                                          K 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