bool v byte

Probably a dumb-ass question.
If I am defining a variable which will only ever be 0 or 1, or TRUE or FALSE, I define is byte xxx = 0;
What, if anything, is the difference between using byte or bool? Both use 1 byte.

TIA
Fof

What, if anything, is the difference between using byte or bool? Both use 1 byte.

When I see a bool variable, I expect it to be assigned values of true or false. Of course, you can assign a bool value any value between 0 and 255, but that doesn't make sense, IMHO. You can also assign true or false to a byte variable, but, again that doesn't make sense, IMHO.

The compiler, and the Arduino, don't give a rat's ass about my opinion.

PaulS
So, correct me if I am wrong.
In the scenario where the variable only ever contains 0 or 1, you would use bool for clarity and byte if the value could be other than 0 or 1.
That to me makes good sense, but in that case why have two different, but identical, data types?
There must be some difference in the operation/execution.
Fof

Of course, you can assign a bool value any value between 0 and 255,

I did not know that. But every reference I've seen says true/false or 1/0 so you're misusing it if you assign a different value.

It seems possible that the compiler could report an error, or some compilers might "pack" 8 bools into a byte.

It is perfectly acceptable to use non-Boolean values in Boolean logic. i.e. Any non-zero byte, integer, etc. will evaluate to "true" and any numerical-zero will evaluate to "false".

DVDdude
That's it, of course.
byte will return a value; bool returns TRUE/FALSE, when it contains either 0 or any value >0.
Fof

IamFof:
What, if anything, is the difference between using byte or bool? Both use 1 byte.

Take it for what you will, but my approach is that byte vs bool only matters in a global or static variable, and in either case, i will use a byte (unless I am holding a value returned by a function that returns a bool), because I am thinking maybe I can expand it's use later on and get more use from the 8-bit size. it terms of bool, I will make bool functions, very handy for if statements. I write these in a way that evaluates and returns a bool, especially where the careless (noob friendly) Arduino functions return an int or some other variable. So, for permanent members of the stack, a byte offers more uses. For local variables, it doesn't really matter, but a bool is better suited to conditional statements. Alternatively, bools can be collectively stored and referenced in a struct so as not to consume an entire address alone.

Perehama
You say

in either case, i will use a byte (unless I am holding a value returned by a function that returns a bool)

So, a byte will return its value, while a bool is interpreted as either 0/1, when it contains either a 0 or a non-0 value.
Am I correct?

This can be tested using Serial.print() :wink:

IamFof:
why have two different, but identical, data types?
There must be some difference in the operation/execution.

You don't write code just for the compiler, you also write it for humans.
Different type names show the intentions of the programmer to other programmers (and his future self).

As mentioned before, bool and byte are not equivalent.

Pieter

IamFof:
Perehama
a bool is interpreted as either 0/1

incorrect. That is the concept behind a bool and it's existance, but, like any variable, it can be cast into a number of roles. If I have

bool b;

I can

b = 3;

I can

if (b) doFoo();

I can also do most of the other things a byte or a char can do, but I need to know how it WILL be interpreted (contrary to it's conceptual purpose).
Alternatively, if I have

byte b;

I can

if (b == 0) goLeft();
else if (b == 1) goRight();
else if (b == 2) goForward();
else if (b == 3) goBackward();
else if (b >= 4) noGo();

if I have

char c;

I can

switch (c) {

You say you have a variable that will only ever be 0 or 1, and you want to know which to use? If you are going to assign 0 or 1, not true or false, you have to answer the question "why?" What will be the most clear to anyone reading the source code you will write? I may have been way too confusing to answer your question and gone way off the deep end without fully reading your question. What is your program and why will you be assigning 0 or 1? How do YOU want to interpret these values? Would a ternary operator be more ideal? If your program expands in scope, will the ability to assign 2-255 benefit you? Will YOU want to interpret them differently?

Perehama:
If I have

bool b;

I can

b = 3;

I can

if (b) doFoo();

I don't have an Arduino plugged-in at the moment but I think if you print you'll find b=1 or b+1 = 2.

Perehama
Yes. You did lose me a bit there :slight_smile: :slight_smile: :slight_smile:

The question was purely that, a question.

For variables whose contents will never exceed 255, I use byte.
For Flags, whose contents never never exceed 1, I also use byte.

If I have a byte variable, A, whose contents are >=1, I can do if(A) { }
If I have a bool A, whose contents are >=1, I can do if(A) { }

I am presuming, hence the original, probably badly explained, question, that if I interrogate byte A, it will return its value, but bool A would return 1 if its contents >=1.

Beyond that, ease of program readability, as mentioned by PaulS

DVDdude and LarryD. I will now just knock up a sketch and see what happens.

Thanks all.

Fof

what makes the bool interesting is you can use the "true" and "false" flag then on your conditional test all you need do is
test if??

example

bool running = true;

if (running) {

// this section would be performed if the bool running flag is true

}

else {

// this section would be performed if the running flag is set to false.

}

Just done a check, and yes. If bool A contains anything >=1, Serial.println(A) returns 1.

Should have done that in the first place, but I thought to pick a few brains, as I often get to pick up other little gems of knowledge, apart from what I was asking about.

Thanks guys

Fof

For documentation purposes:

#define isRunning true
#define isStopped !isRunning

bool  myMachineFlag = isRunning;

void setup()
{
  if (myMachineFlag == isRunning)
  {
    //this section would be performed if: bool  myMachineFlag is isRunning
  }
}

void loop()
{

}

If I recall from the days when dinosaurs walked the earth...
BOOL was originally created to accommodate the state of single bit values or returned states.

They could be ‘set’ or ‘not-set’ leading to the perception and convenience of TRUE and FALSE terminology.

I hope that in more advanced compilers on larger systems that concept is still observed (oddly enough to conserve memory!)

Perhaps no-one gives a shit any more. Too hard in this era of social media?
It’s the same fundamental that was used to evaluate non-zero or zero values as TRUE / FALSE.

Who knows?

Another difference is that bool is part of the C++ language specification (and thus all its behavior is also described there), while 'byte' is just a typedef. By convention, it is the smallest addressable unit of storage, unsigned, etc, but it is not part of the standard.

The C++ standard defines std::byte which provides some actual guarantees about its representation and usage.

whoa, i always assumed that the compiler and the flash held multiple bools in a byte in the background somewhere. ive been using bools for proficeincy. you guys are saying that true bools that take one bit dont exist anymore? is this also true with other languages like C# or is this compiler laziness exclusive to arduino?

In almost all cases (embedded programming being an exception) speed is optimized over size and it is much faster for a bool to be a byte. The bit twiddling is not free, and as a bit field there's a lot of coercion from whatever power of two that bool is to the value 1.

For example

if (bool1 == bool2)
{
    ..blah blah blah
}

or even

if (bool1 & bool2)
     ...blah blah blah

Trivial for the bool-as-a-byte representation, twiddle-rific for the bit-field representation. The compiler provided 'bool' has a lot of restrictions placed upon it by the C++ specification.