How to print all the 8 bits of a byte on serial monitor, if they are all zero

Hi all,

As per title...
Assuming I have this variable: mybyte = 00000000.

Whenever I print

Serial.println(mybyte, BIN);

I get 0 on the serial monitor. How would I change this so as to print the values of all the 8 bits?

Thanks

A for loop.

AWOL:
A for loop.

and the bitRead() function

I decided to do a little test.
A 'for' loop shift wins by 4 bytes of program space. Both use the same RAM.

I commented out each method in turn:-

void setup()
{
    // Common lines:-
    Serial.begin(115200);
    byte val = 16;

    // 2124 program / 182 RAM:-
    for (int i = 0; i < 8; i++)
    {
        bool b = val & 0x80;
        Serial.print(b);
        val = val << 1;
    }

    // or

    // 2128 program / 182 RAM:-
    for (int i = 7; i >= 0; i--)
    {
        bool b = bitRead(val, i);
        Serial.print(b);
    }
}

void loop(){}
1 Like

If you just want to display all the bits for your own debugging purposes a crude and simple solution would be to add the byte to an int containing 0b0000000100000000 (i.e. an extra bit) and then print the int. It will show as 10000000 and you can ignore the 1.

int printVal = 0b0000000100000000;
printVal += (int) myByte;
Serial,println(printVal, BIN);

...R

Robin2:
If you just want to display all the bits for your own debugging purposes a crude and simple solution would be to add the byte to an int containing 0b0000000100000000 (i.e. an extra bit) and then print the int. It will show as 10000000 and you can ignore the 1.

int printVal = 0b0000000100000000;

printVal += (int) myByte;
Serial,println(printVal, BIN);




...R

Surprisingly, that uses more program memory and RAM than the other methods:-
(I didn't expect that. :smiley: )
2142 bytes program memory, 200 bytes RAM:-

void setup()
{
    Serial.begin(115200);
    byte myByte = 15;
    int printVal = 0b0000000100000000;
    printVal += (int) myByte;
    Serial.println(printVal, BIN);
}

void loop() {}

OldSteve:
Surprisingly, that uses more program memory and RAM than the other methods:-
(I didn't expect that. :smiley: )

You are way ahead of me. I didn't even think about that.

I did say it was a crude and simple solution :slight_smile:

...R

#include <limits.h>

template<typename T>
void printBinary(T value)
{
    for ( size_t mask = 1 << ((sizeof(value) * CHAR_BIT) - 1); mask; mask >>= 1 )
    {
        Serial.print(value & mask ? "1" : "0");
    }
}

void loop()
{   }

void setup()
{
    Serial.begin(9600);
    
    const short DEAD = 0xDEAD;
    printBinary(DEAD);

    Serial.println("");

    const uint32_t DEADBEEF = 3735928559UL;
    printBinary(DEADBEEF);

    Serial.println("");

    const uint8_t N = 0b00001010;
    printBinary(N);
}

Robin2:
You are way ahead of me. I didn't even think about that.
I did say it was a crude and simple solution :slight_smile:
...R

I'm often shocked by the size that various code compiles down to, (or doesn't compile down to).
I've been comparing a lot of things lately.

lloyddean:

#include <limits.h>

template
void printBinary(T value)
{
   for ( size_t mask = 1 << ((sizeof(value) * CHAR_BIT) - 1); mask; mask >>= 1 )
   {
       Serial.print(value & mask ? "1" : "0");
   }
}

void loop()
{   }

void setup()
{
   Serial.begin(9600);
   
   const short DEAD = 0xDEAD;
   printBinary(DEAD);

Serial.println("");

const uint32_t DEADBEEF = 3735928559UL;
   printBinary(DEADBEEF);

Serial.println("");

const uint8_t N = 0b00001010;
   printBinary(N);
}

This won't compile for me. I get:-

variable or field 'printBinary' declared void

If I give it a return type, then I get:-

'T' was not declared in this scope

OldSteve:
This won't compile for me. I get:-

variable or field 'printBinary' declared void

If I give it a return type, then I get:-

'T' was not declared in this scope

Builder problem, use

template<typename T> void printBinary(T value)
for(int i = 7; i>=0;i--) {
  Serial.print((char)('0' + ((b>>i)&1)));
}
Serial.println();
1 Like

oqibidipo:
Builder problem, use

template<typename T> void printBinary(T value)

Still the same error "variable or field 'printBinary' declared void"
That's with IDE V1.6.5.

Not to worry, though, the OP has plenty to choose from now, and I suspect that Paul's method might be the most code-efficient. (Too busy to test more right now.)

Tis interesting as it compiles and runs for me on 5 different IDE's!

This self adapts to the data type passed to it it, not simply 8-bit values.

As to some of the other solutions presented -

I can't understand why people continue to write these loops so awkwardly.

The bit size of a 'char' is given by the include file 'limits.h' as 'CHAR_BIT' which on a LOT of platforms, but not all, as 8.

At the minimum -

uint8_t    data = 0b10101111;

for ( size_t mask = (1 << CHAR_BIT); mask >>= 1 ; )
{
 Serial.print(((data & mask) '1' : '0');
}

Serial.println();

lloyddean:
Tis interesting as it compiles and runs for me on 5 different IDE's!

Not here, on V1.6.5.

I can't understand why people continue to write these loops so awkwardly.

Not everyone has the same level of experience with C++, and many of us aren't/weren't even aware of the existence of "limits.h". This is primarily a beginners forum, of which I am one.

People do the best they can with the knowledge they have.

And at least all other methods listed compile with my IDE.

Steve, I really do understand that but, I'm trying to get a rise out people such that they get something more out of the questions asked than simply the answer to what was asked.

You're right, my apologies for adding to the confusion!

it doesn't compile on the latest Arduino IDE, I've tested it on what appears to be a couple of versions out of date.

I continue to be disappointed by the preprocessing feature of the IDE. I realize it's intent to make things easier for the beginner but wish there were a way to turn it off.

lloyddean:
You're right, my apologies for adding to the confusion!

it doesn't compile on the latest Arduino IDE, I've tested it on what appears to be a couple of versions out of date.

I was just in the middle of a reply when I spotted this. Thanks, at least now I know it's not a problem with my installation.

I continue to be disappointed by the preprocessing feature of the IDE. I realize it's intent to make things easier for the beginner but wish there were a way to turn it off.

Yeah, it really is a beginners platform. I have Atmel Studio installed, but haven't played with it much yet. (The Arduino IDE so easy to use that I'm reluctant to make the jump. :slight_smile: )

Anyway, not to worry - in reality the difference between code size for the various methods of doing this are hardly worth worrying about. I originally just set out to do a test or two out of interest.

My current favorite Arduino compatible IDE is UECIDE: The Universal Embedded Computing IDE.

http://uecide.org

It supports multiple Arduino compatible targets other than just Arduino.

The IDE also has preferences for disabling the preprocessing feature I HATE on the current Arduino IDE allowing me to write correct code and enable seeing proper errors and warnings, including the interference keeping the templated function I posted from compiling.

lloyddean:
My current favorite Arduino compatible IDE is UECIDE: The Universal Embedded Computing IDE.

http://uecide.org

It supports multiple Arduino compatible targets other than just Arduino.

The IDE also has preferences for disabling the preprocessing feature I HATE on the current Arduino IDE allowing me to write correct code and enable seeing proper errors and warnings, including the interference keeping the templated function I posted from compiling.

I've looked at a couple of alternative IDEs, but only use UNO or stand-alone '328P, tiny84 and tiny85 chips, so haven't worried about trying them. I've yet to even advance to a Mega2560.

I'm keeping a link to EUCIDE, though, in case I feel ambitious or start writing for other targets.
(I'm really a long-term PIC/MPLAB man, and only made the switch 4 or 5 months ago.)

I have a preference for the Arduino compatible Digilent PIC32 based boards myself, more RAM, FLASH ROM, 50 to 80 MHz a lot more capable then all but some of the more recent Arduino boards.

MPLAB X is what I've been working with, but it's a problematic work in progress itself as well.