# How to read a full byte value in binary?

Is there a way to have a print function where the entire byte is shown?
if i do:
byte a = 1;
Serial.println( a , BIN);
i will get a serial output of "1" However i would like an output of "00000001"

Mark

TheHonestGuy:
Is there a way to have a print function where the entire byte is shown?
if i do:
byte a = 1;
Serial.println( a , BIN);
i will get a serial output of "1" However i would like an output of "00000001"

You can add code to print those zeros.
.

Something a bit more flexible -

#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);

Serial.println("");

Serial.println("");

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

However i would like an output of "00000001"

Why? How does 00000001 differ in any meaningful way from 1?

Not different in a meaningful way but very different in a visual way, particularly if you are printing a series of values one under the other.

void setup()
{
Serial.begin(115200);
for (byte x = 0; x <= 50; x++)
{
byte x = random(0, 255);
printBinary(x);
Serial.print("\t");
Serial.print(x);
Serial.println();
}
}

void loop()
{
}

void printBinary(byte x)
{
for (int bit = 7; bit >= 0; bit--)
{