-
February 18th, 2021, 02:29 PM
#1
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 ?
-
February 18th, 2021, 04:33 PM
#2
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?
-
February 18th, 2021, 09:41 PM
#3
Re: is it safe to use the bitwise XOR for boolean types (in c++)
Originally Posted by pdk5
what is best way to get logical xor for booleans ?
It is best to use the logical XOR operator != whereby
becomes
Code:
bool c = !(a != b);
// alternatively
bool c = (a == b);
Last edited by wolle; February 18th, 2021 at 10:47 PM.
-
February 19th, 2021, 02:36 AM
#4
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
-
February 19th, 2021, 05:01 AM
#5
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)
-
February 19th, 2021, 05:32 AM
#6
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
-
February 19th, 2021, 05:51 AM
#7
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
-
February 19th, 2021, 06:11 AM
#8
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)
-
February 19th, 2021, 08:23 AM
#9
Re: is it safe to use the bitwise XOR for boolean types (in c++)
Originally Posted by 2kaud
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.
-
February 19th, 2021, 08:32 AM
#10
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
-
February 19th, 2021, 08:48 AM
#11
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
-
February 19th, 2021, 08:50 AM
#12
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)
-
February 19th, 2021, 09:25 AM
#13
Re: is it safe to use the bitwise XOR for boolean types (in c++)
Originally Posted by 2kaud
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
-
February 19th, 2021, 12:08 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|