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. BOOL to bool conversion

BOOL to bool conversion

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancecssvisual-studioquestion
10 Posts 5 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.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type bool and has decided to continue using it when developing MFC apps. From MSDN, I understood that bool type variable would occupy less memory than BOOL (1 bytes vs. 2 or 4 bytes), since BOOL is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:

    warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

    performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?

    BOOL bTest;
    
    if (bParameter == true)
    	bTest = TRUE;
    else
    	bTest = FALSE;
    

    Thanks!

    L T 2 Replies Last reply
    0
    • J J B 0

      Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type bool and has decided to continue using it when developing MFC apps. From MSDN, I understood that bool type variable would occupy less memory than BOOL (1 bytes vs. 2 or 4 bytes), since BOOL is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:

      warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

      performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?

      BOOL bTest;
      
      if (bParameter == true)
      	bTest = TRUE;
      else
      	bTest = FALSE;
      

      Thanks!

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

      BOOL is indeed a typedef'd int. If you want to do a safe conversion do this: bTest = (bParameter != FALSE); I don't know what kind of performance decrease you get when directly casting a bool to a BOOL. regards

      1 Reply Last reply
      0
      • J J B 0

        Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type bool and has decided to continue using it when developing MFC apps. From MSDN, I understood that bool type variable would occupy less memory than BOOL (1 bytes vs. 2 or 4 bytes), since BOOL is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:

        warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

        performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?

        BOOL bTest;
        
        if (bParameter == true)
        	bTest = TRUE;
        else
        	bTest = FALSE;
        

        Thanks!

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        You have it backwards, the performance hit is when you do a BOOL to a bool. bool bOut = bIn != FALSE; BTW, with a BOOL, never do (x == TRUE) as a test. As far as conditionals go, it is 0 or not 0. It doesn't test for FALSE and TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        J 1 Reply Last reply
        0
        • T Tim Smith

          You have it backwards, the performance hit is when you do a BOOL to a bool. bool bOut = bIn != FALSE; BTW, with a BOOL, never do (x == TRUE) as a test. As far as conditionals go, it is 0 or not 0. It doesn't test for FALSE and TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.

          J Offline
          J Offline
          J B 0
          wrote on last edited by
          #4

          Thanks alot guys, the comments are very useful. Tim, are you saying a BOOL type variable can be more than just TRUE (1) and FALSE (0) ?

          L V 2 Replies Last reply
          0
          • J J B 0

            Thanks alot guys, the comments are very useful. Tim, are you saying a BOOL type variable can be more than just TRUE (1) and FALSE (0) ?

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

            actually, yes. But I've not seen any app setting the value of a BOOL to anything other apart from TRUE and FALSE. TRUE is just a #define (1), while FALSE is a #define (0)

            A 1 Reply Last reply
            0
            • L Lost User

              actually, yes. But I've not seen any app setting the value of a BOOL to anything other apart from TRUE and FALSE. TRUE is just a #define (1), while FALSE is a #define (0)

              A Offline
              A Offline
              A T I F
              wrote on last edited by
              #6

              Don;t be too sure about it... TrackPopupMenu return BOOL and it can be made to return which menu item was selected in the popup menu (setting via parameters).

              L 1 Reply Last reply
              0
              • J J B 0

                Thanks alot guys, the comments are very useful. Tim, are you saying a BOOL type variable can be more than just TRUE (1) and FALSE (0) ?

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                if your BOOL > 0 it is TRUE. if your BOOL = 0 it is FALSE. Don't know for sure for values < 0. their are apps eg. where your BOOL has value 2 or 3 , ... then you can do if(YOURBOOL){ ... } personally I think BOOL is against the whole idea of the basic datatype of a boolean. I would recommend a bool if you want to use true or false and an int or something if you have more values (and do if(yourint == 3){...}) but it's MFC :~ "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

                T 1 Reply Last reply
                0
                • A A T I F

                  Don;t be too sure about it... TrackPopupMenu return BOOL and it can be made to return which menu item was selected in the popup menu (setting via parameters).

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

                  Thanks; that's why I said "I haven't seen any app....". Now I know there IS at least one API doing such things :) regards

                  T 1 Reply Last reply
                  0
                  • V V 0

                    if your BOOL > 0 it is TRUE. if your BOOL = 0 it is FALSE. Don't know for sure for values < 0. their are apps eg. where your BOOL has value 2 or 3 , ... then you can do if(YOURBOOL){ ... } personally I think BOOL is against the whole idea of the basic datatype of a boolean. I would recommend a bool if you want to use true or false and an int or something if you have more values (and do if(yourint == 3){...}) but it's MFC :~ "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    it is BOOL == 0 is FALSE BOOL != 0 is TRUE Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Thanks; that's why I said "I haven't seen any app....". Now I know there IS at least one API doing such things :) regards

                      T Offline
                      T Offline
                      Tim Smith
                      wrote on last edited by
                      #10

                      The WIN32 API is a bit of a minefield when it comes to BOOL processing. On NT the API might return a true TRUE/FALSE while on Win9x, then API might return ==0/!=0. This is why I always say to play it safe and never test for "== TRUE". Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      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