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 / C++ / MFC
  4. Comparing BOOL values

Comparing BOOL values

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 9 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.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

    N A T R A 8 Replies Last reply
    0
    • A Abin

      Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      =[ Abin ]= wrote: if ((b1 && b2) || (!b1 && !b2)) I don't think it looks that ugly, but you could always write a function to handle the comparison for you. -Nick Parker

      1 Reply Last reply
      0
      • A Abin

        Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

        A Offline
        A Offline
        Angel Kid
        wrote on last edited by
        #3

        Maybe you can write your BOOL class and overload the operator "==". YES, I am here.

        1 Reply Last reply
        0
        • A Abin

          Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

          T Offline
          T Offline
          Tibor Blazko
          wrote on last edited by
          #4

          (!b1) != (!b2) looks better? t!

          1 Reply Last reply
          0
          • A Abin

            Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            I agree with Nick. If you think it looks ugly, write a function (make it inline for speed...). I don't think it looks ugly and is exactly what I would do. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
            Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

            1 Reply Last reply
            0
            • A Abin

              Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              I think the == operator disregards the "non zero" value, just assums it like "1". If that happnes, "if (b1 == b2)" is absolutly correct

              1 Reply Last reply
              0
              • A Abin

                Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

                RaviBeeR Offline
                RaviBeeR Offline
                RaviBee
                wrote on last edited by
                #7

                I don't think what you wrote is ugly. I'd only add an explicit comment before the if.

                // Do "something" if b1 and b2 are both true or both false
                if ((b1 && b2) || (!b1 && !b2)) {
                something;
                } else {
                something else;
                }

                /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

                1 Reply Last reply
                0
                • A Abin

                  Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

                  A Offline
                  A Offline
                  Anders Molin
                  wrote on last edited by
                  #8

                  That's why I always use bool instead of BOOL ;) - Anders Money talks, but all mine ever says is "Goodbye!"

                  1 Reply Last reply
                  0
                  • A Abin

                    Sometimes I need to compare two boolean values, for example: BOOL b1 = condition1(); BOOL b2 = condition2(); if (b1 == b2) // both TRUE or both FALSE { // something } else { // something else } Above code makes sense but actually I guess they are wrong, since BOOL's are actually INT's, while "false" is guaranteed to be "0", "true" is only guaranteed to be "non-zero" ---- in theory, it may or may not be "1", so even if b1 and b2 are both true, they don't necessarily equal to each other. Although I can do this instead: if ((b1 && b2) || (!b1 && !b2)) { // something } else { // something else } This sure will work but looks ugly, my question is, how to compare two BOOL values in a way that the code works AND looks not ugly?

                    C Offline
                    C Offline
                    Chris Richardson
                    wrote on last edited by
                    #9

                    I would use bool, but I don't think the following code is all that bad:

                    // Now, if b1 or b2 is anything other than 0, it will become 0, otherwise it will become 1, so the == operator works as expected.
                    if( !b1 == !b2 )
                    {
                    // Something.
                    }
                    else
                    {
                    // Something else.
                    }

                    Chris Richardson
                    Terrain Software

                    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