The most efficient way to test whether a byte is "empty" (B0000000) or not?

I have completed a small project I did to challenge myself to learn more about my arduinos programming. In this project, inside a for loop I have to check whether a MyVar byte fetched from an array of bytes, is "empty" (or `0`, or `B0000000` what applies best). Rough description of the program in note 1.

I did a lot of trial and error, and quickly realized that just asking

``````byte MyVar = B0001111
if {MyVar = 0 ; DoSomethingSpecial() } else {ProceedLoop()}
``````

did not produce anything near the desired state. I deduced that testing bytes probably need to happen bitwise, or something clever (=beyond my understanding) is going on in how the compiler interprets the conversion.

So I created a dummy variable byte

``````byte EmptyValue = B0000000
``````

and then did a logical `AND` between the MyVar and the EmptyValue

``````if (MyVar&EmptyValue) {DoSomethingSpecial(); } else {ProceedLoop(); }
``````

which seemed to do the trick - it was getting late and my ability to find useful Serial.print() to capture the different values was getting low, but the results running the program were at the time indistinguishable to me from the expected way.

My question, firstly - is this a reasonable way to check if a byte is zero? Does it actually do that? I have been reading up on these bitwise calculations and I am truly confused to how it compares a final `TRUE` or `FALSE` from comparing.

Secondly, what would be a more processing-time-optimal way of doing this comparison? Is there a null-test of sorts? Should I have cast it into integer and tested against integer 0?

``````  const int interval=400;
const byte ByteArrayMakeSound[11]={
B0001111,                                       //H - length 4
B0001111,                                       //E - length 4
B0001111,                                       //L - length 4
B0001111,                                       //L - length 4
B0000111,                                       //O - length 3
B0000000,                                       //
B0000111,                                       //W - length 3
B0000111,                                       //O - length 3
B0000111,                                       //R - length 3
B0001111,                                       //L - length 4
B0000111                                        //D - length 3
};
const byte ByteArrayLongShort[11] =   {
B0001111,                                       //H = --- --- --- ---
B0000001,                                       //E = o o o ---
B0001011,                                       //L = --- o --- ---
B0001011,                                       //L = --- o --- ---
B0000000,                                       //O = o o o
B0000000,                                       // (pause 7 intervals)
B0000100,                                       //W = --- o o
B0000000,                                       //O = o o o
B0000101,                                       //R = --- o ---
B0001011,                                       //L = --- o --- ---
B0000011                                        //D = o --- ---
};

void setup()
{
pinMode (LED_BUILTIN, OUTPUT);
}

void loop()
{
for (byte x = 0; x < 10; ++x) {
byte sound = ByteArrayMakeSound[x];
byte longshort = ByteArrayLongShort[x];
byte emptyvalue {B0000000};
if (sound&emptyvalue) {
delay(7 * interval);
}    else    {
for (byte y = 0; y < 7; ++y)  {
if (sound & (1<<y)){
if (longshort & (1<<y))     {
MakeSound(3);
delay(interval);
}   else    {
MakeSound(1);
delay(interval);
}
}   else      {}
}
}
delay(3 * interval);
}
}

void MakeSound(int del) {
digitalWrite(LED_BUILTIN, HIGH);
delay(interval * del);
digitalWrite(LED_BUILTIN, LOW);
}
``````

Note 1: Program purpose

The program started as a hello world program with me trying to write "hello world" in morse code with the builtin led. The two byte arrays store the "letter length" of the morse and the "daa" / "dit" assignment. I use empty bytes to separate words, which means a delay of 3 intervals (= length of "daa") should happen. Then including the entire letter-table of morse and a string to byte table conversion (to create the byte arrays). It is not very quick... But I am tinkering with moving the byte arrays into the flash memory. Just for Hello World it is fast enough, but the "daas" and the "dits" are slightly uneven so programming time is impacting performance.

• "=" is an assignment; "==" is a equality comparison. You are missing the semi-colons. Logical AND is "&&", not "@". That's not C, nor any know programming language.
– user31481
Commented Nov 2, 2017 at 11:44
• Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. Minimal means you've stripped away irrelevant stuff, just leaving what's needed to show the problem. Complete means all the library names are shown, all the variable declarations, and all the function definitions – so people don't have to waste time guessing what you did or what you meant. Verifiable means it can be compiled and tested, allowing other people to test their theories about the problem
– user31481
Commented Nov 2, 2017 at 11:44
• "&" is the bit-wise AND operator. "&&" is the logical AND operator. "A&B" is not the same that "A&&B".
– user31481
Commented Nov 2, 2017 at 12:41
• Get a copy of "The C Language 2° Edition" by K&R. It's a short book. Read it from cover to cover. I think you are smart enough, but lacking formal training. You also need some introductory text to computers (in general). Cheers!
– user31481
Commented Nov 2, 2017 at 12:48
• @LookAlterno Thanks for the tip, let's hope Amazon delivers as quickly as stackexchange =) Commented Nov 2, 2017 at 13:08

The fast way is:

``````byte x;
...
if (x) {
// not zero.
} else {
// zero
}
``````

You are barking at the wrong tree.

• So an empty byte itself is interpreted as false? Thats efficient. Commented Nov 2, 2017 at 12:06
• There is no such thing as an empty byte (int, long, float, ...). Every byte (int, long, float, ...) has a value inside. Value can be zero, but that's is not empty. Zero is a legal value, just like any other.
– user31481
Commented Nov 2, 2017 at 12:36
• Yes, a zero valued byte, not empty. Gotcha. It was just not obvious to me that simply asking if directly on a byte variable type returns false if the byte happens to have only zeroes. Like for strings, even if a string value will be interpreted as zero, it often doesn't compute inside an if. Thank you for clearing this up. I'll keep the question open for another 24h as per the rules. Commented Nov 2, 2017 at 12:59
• @StianYttervik. Be aware that strings and Strings in C/C++ doesn't work exactly like strings in JavaScript, PHP or any other language. There are some differences that will bite you hard.
– user31481
Commented Nov 2, 2017 at 13:02
• Try Serial.println(true); and Serial.println(false); to get a better understanding of why this works. ;) Commented Nov 3, 2017 at 12:32