Can you set boolean to 3?

Hello, can you set boolean to 3?

True, false and , , , ?
(Yes, you probably can, but why?)

The reference says:

A boolean holds one of two values, true or false.

Many things are possible in C and the results are sometimes called "indeterminate".

I can think of a 4 value boolean: true, false, not set and don't care.

And yet George himself conceived only two.

Logic circuit data sheets are full of don't cares.

For the OP:
It is probably best to create a byte and use some #defines 'MY_TRUE, MY_FALSE, MY_WHATEVER' to test them.

You can not assign a 3 to a boolean directly, it will be folded to 1 in this process.
Any non-zero value will be interpreted as true, when used as a boolean expression.

union  {
  bool logic;
  byte value;
} test;

void printIt() {
  Serial.print(F("logic "));
  Serial.print(test.logic);
  Serial.print(test.logic ? F(" (true)") : F(" (false)"));
  Serial.print(F(", value "));
  Serial.println(test.value);
}

void setup() {
  Serial.begin(250000);
  test.logic = 3;
  Serial.print(F("test.logic = 3; -> "));
  printIt();
  test.value = 3;
  Serial.print(F("test.value = 3; -> "));
  printIt();
}
void loop() {}
test.logic = 3; -> logic 1 (true), value 1
test.value = 3; -> logic 3 (true), value 3

Yes, No, Maybe so?

-jim lee

KeithRB:
Logic circuit data sheets are full of don't cares.

Those aren't actual values though, just a shorthand notation to compress the size of the table.

OK then: High, Low and High-Z. 8^)

Why. Would you need. Do do that?

Really, people. We're supposed to be helping beginners; could we please not go off on obscure tangents just so you can demonstrate your supposed brilliance in semi-obscure electronics or computer language internals...

can you set boolean to 3?

No.

Boolean variables are defined to have one of two states (true or false.) DEFINED!!! "Boolean arithmetic" defines the properties of "math" with numbers that can only have two values.
C++ (the "Arduino language") has true support for boolean variables, so a statement like:

  boolean b = 3;

will result in b containing "true"

Now, in C++, the minimum size for any variable is one byte, so there are some tricky ways that you could put "3" in the byte that represents a boolean variable. And in C and some older languages, "boolean" was just another name for "byte", and "b = 3;" would in fact put 3 in b. But these are "obscure details."

westfw:
Really, people. We're supposed to be helping beginners; could we please not go off on obscure tangents just so you can demonstrate your supposed brilliance in semi-obscure electronics or computer language internals...
No.

...
...

Now, in C++, the minimum size for any variable is one byte, so there are some tricky ways that you could put "3" in the byte that represents a boolean variable. And in C and some older languages, "boolean" was

...
...

You were going so well there! But no.. Looks like you caved in the end.

Brilliant! :slight_smile:

-jim lee

"On two occasions I have been asked, — "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" In one case a member of the Upper, and in the other a member of the Lower, House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."

~ Charles Babbage, Passages from the Life of a Philosopher (1864), ch. 5 "Difference Engine No. 1"

jimLee:
You [westfw] were going so well there! But no.. Looks like you caved in the end.

Loved the irony in westfw's post.

lukapetko2:
Hello, can you set boolean to 3?

My reaction when I see questions like this is: who not try it?

void setup() 
{
bool foo = 3;
Serial.begin (115200);
Serial.println (foo);
}

void loop()  { }

That compiles without warnings or errors, so it appears you can attempt to set a boolean to 3.

However if you look at the serial output you see:

1

Since a bool can hold true or false, it seems illogical to expect it to hold some other value, and somewhat weird to even attempt it. It's like saying: can I put "hello world" into a float?

I’d suggest using data type byte vs bool, then any value from 0 to 255 can be used.

Bool uses 1 byte of memory, same as data type byte.

AWOL and PaulMurrayCbr asked "why?". This is the crux of the issue.

If the OP wishes to do this, then there must be a problem they are trying to solve that makes this seem like the likely answer. This is the essence of an XY problem. I would posit that if one needs to set a boolean variable to something other than true or false then it probably shouldn't have been a boolean.

So lukapetko2, can you tell us what you are doing that needs a boolean to be 3?

Could use two Booleii for a total of four combinations :wink:

Really, the whole thing is an X-Y problem.