Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

behavior of bool with non-boolean operators

What I really want is a ||= operator.

old_value = old_value || possible_new_value;
old_value ||= possible_new_value;

The second line is a compiler error (c++ doesn't have a ||= operator).

So what are my other options?

old_value += possible_new_value;
old_value |= possible_new_value;

While I'm on the subject how does bool behave with other non-boolean operators?

-
-=
&
&=
...

I can verify these empirically, but I'm most interested in what the standard says.

like image 519
deft_code Avatar asked Aug 18 '09 16:08

deft_code


People also ask

What is Boolean and non Boolean?

While a Boolean two-valued logic with truth values “true” and “false” is best characterized by the famous “rule of the excluded middle” (or tertium non datur), non-Boolean logic violates this rule. The consequence is incompatible descriptions, which are central to the notion of complementarity.

What are non Boolean values?

The values "false", "FALSE", "0", "no", and "NO" will be considered boolean FALSE; all other strings will be considered boolean TRUE.

How do you use NOT operator Boolean?

If you need to search the word not, that can usually be done by placing double quotes (<< >>) around it. Example: A search on Mexico AND NOT city includes results contains: New Mexico; the nation of Mexico; US-Mexico trade; but does not return Mexico City or This city's trade relationships with Mexico.

Is a bool A Boolean?

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.


1 Answers

According to 4.7 (Integral conversions), paragraph 4, "If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and the value true is converted to one." In 4.12, "An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true."

In a context where bool operands are not allowed but integral operands are, the bool will be converted to an integral type. When the integer result is stored in a bool variable, it will be converted to bool.

Therefore, you will be able to use + and * as boolean or and and, and you can use | and & also. You can't get away with mixing them, as (bool1 + bool2) & bool3 will yield false if all three variables are true. ((1 + 1) & 1 is 2 & 1, which is 0, or false.)

Remember that | and || don't work identically even here. | will evaluate both sides, and then evaluate the bitwise or. || will evaluate the first operand, then only if that was false will evaluate the second.

I'm not going to discuss the stylistic issues here, but if I did anything like that I'd be sure to comment it so people knew what I was doing and why.

like image 195
David Thornley Avatar answered Oct 21 '22 05:10

David Thornley