CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2015
    Posts
    500

    is it safe to use the bitwise XOR for boolean types (in c++)

    Hello

    lets a i have :

    bool a = true;

    bool b = false;

    is it safe to use

    bool c = !(a^b)

    Are the other 7 bits guaranteed to be same for true and false. Will it have issues when moved b/w different OSs.

    If it cannot be, what is best way to get logical xor for booleans ?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    What are you trying to accomplish by doing that? What's wrong with logical or?

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Quote Originally Posted by pdk5 View Post
    what is best way to get logical xor for booleans ?
    It is best to use the logical XOR operator != whereby

    Code:
    bool c = !(a^b);
    becomes

    Code:
    bool c = !(a != b);
    
    // alternatively
    
    bool c = (a == b);
    Last edited by wolle; February 18th, 2021 at 10:47 PM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Microsoft compiler represents bool values as one-byte values being set
    either to 0x00 (false)
    or to 0xFF (true)

    So you can test all the three combination to check whether it works.
    How is boll represented by other C++ compilers you should check yourself.

    However, as others have already pointer out, bool and byte are different types, so one should use logical operators with Boolean value types!
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    is it safe to use the bitwise XOR for boolean types (in c++)
    In C++, true is 1 and false is 0. This can be shown by:

    Code:
    bool z = true;
    
    printf("%02x\n", (unsigned char)z);
    which displays 01 as expected.

    However, if you do:

    Code:
    bool z = true;
    
    printf("%02x\n", (unsigned char)z);
    
    z = 15;
    
    printf("%02x\n", (unsigned char)z);
    then it still displays:

    01
    01

    showing that the values of a bool are only 1 and 0 - irrespective as to what values they are set.

    Hence, the result of any bitwise operators on a bool type are still going to result in either a value of 1 or 0.

    Now consider:
    Code:
    bool a = true;
    bool b = false;
    auto c = a^b;
    What is the type of c? If anyone says bool - go and stand in the corner! The type of c is int! So the type of a ^ b is int - even though a and b are of type bool!

    So given the values of a and b, then c has the value 1 (as an int).


    Code:
    bool a = true;
    bool b = false;
    auto c = !(a^b);
    In this case, c is of type bool as ! returns a type bool. a ^ b has a value of 1 (as type int). So !1 gives 0, so c is of type bool with a value false.

    Sorry for the long winded explanation, but this needs picking apart to understand.
    Last edited by 2kaud; February 19th, 2021 at 05:04 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2015
    Posts
    500

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    @kaud: Thankyou very much for the detailed explanation. Very helpful.

    Also thankyou wolle and victor for the inputs

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Well 2kaud was right.
    true value is 0x01, not 0xFF.

    I was wrong. Sorry!
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    All sins forgiven
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2015
    Posts
    500

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Quote Originally Posted by 2kaud View Post
    All sins forgiven
    Sorry i meant, i am newbie and learning.

    @Victor: It was so helpful to get inputs and help us think about the issues in depth. Any feedback and help is very much appreciated. I feel so valued and privelaged to get help from c++ gurus here, who have time and patience to help me despite my poor c++. thanks a lot for everybody who try to help me and donot ignore me
    Last edited by pdk5; February 19th, 2021 at 08:47 AM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Some additional info:
    There is also VARIANT_BOOL type.
    For this type the "true" value VARIANT_TRUE is defined as 0xFFFF
    and the "false" one VARIANT_FALSE as 0x0000
    Victor Nijegorodov

  11. #11
    Join Date
    May 2015
    Posts
    500

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Thanks a lot Victor again , I'll check this, very helpful

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Yes - for OLE automation. There's quite a few different types - BSTR possibly being the most well known. They are not part of C++ though. These were developed by MS to help with interoperability with Visual Basic which uses -1 for true and 0 for false (and stores string length not a terminating null). Just for info for anyone reading this.
    Last edited by 2kaud; February 20th, 2021 at 05:20 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Quote Originally Posted by 2kaud View Post
    Yes - for OLE automation. There's quite a few different types - BSTR possibly being the most well known. They are not part of C++ though. These were developed by MS to help with interoperability with Visual Basic which uses -1 for true and 0 for false (and stores string length not a terminating null).
    You are of course correct. Is is the MS feature.
    I have added this info just because this is
    Forum: Visual C++ Programming
    Ask questions about Windows programming with Visual C++ and help others by answering their questions.
    So Visual C++ means "it is Microsoft specific"
    Victor Nijegorodov

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: is it safe to use the bitwise XOR for boolean types (in c++)

    Yeah - Several C++ questions asked in this forum should be asked in the C++ (Non-visual C++ Issues) forum.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured