Comparing BOOL values
-
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?
-
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?
=[ 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
-
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?
-
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?
(!b1) != (!b2) looks better? t!
-
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?
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" -
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?
-
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?
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
-
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?
That's why I always use
bool
instead ofBOOL
;) - Anders Money talks, but all mine ever says is "Goodbye!" -
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?
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