# Leading zeros for binary in serial monitor / console

Hi!

Found only longer version of printing leading zeros to binary number, so here is mine for 16bit:

``````for (int i=0;i<16;i++)
{
Serial.print((data >> i) & 1 == 1 ? "1" : "0"); // will reverse bit order!
}
Serial.print();
``````

vito:
Hi!

Found only longer version of printing leading zeros to binary number, so here is mine for 16bit:

While your code worked well, the binary number felt backwards backwards with the leading zeros being on the right hand side. Do you have a way to reverse the binary number to have the leading zeros on the left.
I tried to think of a simpler way like using itoa(ChrArray,Bin,2) Then adding zeros to the left but I still needed to discover the Length,
I came up with this code and included your code as a comparison.
I made a small change to your code, I think Serial.write is more efficient for printing 1 char to the serial buffer but I’m not sure.

``````void print_binary(int number, byte Length){
static int Bits;
if (number) { //The remaining bits have a value greater than zero continue
Bits++; // Count the number of bits so we know how many leading zeros to print first
print_binary(number >> 1, Length); // Remove a bit and do a recursive call this function.
if (Bits) for (byte x = (Length - Bits); x; x--)Serial.write('0'); // Add the leading zeros
Bits = 0; // clear no need for this any more
Serial.write((number & 1) ? '1' : '0'); // print the bits in reverse order as we depart the recursive function
}
}

void setup() {
Serial.begin(115200);
}

void loop() {
static int data  = 0;
data++;
print_binary(data , 16);
Serial.print(" or ");
for (int i = 0; i < 16; i++) {
Serial.write((data  >> i) & 1 ? '1' : '0');
}
Serial.println();
delay(100);
}
``````

I have wanted a function or a way that would print leading zeros in binary numbers out cleanly thanks @vito for your example It inspired me to code this.
Z

``````void oldFunc(int var) {
for (int i = 0; i < 16; i++)  {
Serial.print(((var >> i) & 1) == 1 ? "1" : "0");
}
Serial.println();
}

void newFunc(int var) {
for (unsigned int test = 0x8000; test; test >>= 1) {
Serial.write(var  & test ? '1' : '0');
}
Serial.println();
}

void setup() {
Serial.begin(250000);
for (unsigned int test = 0x8000; test; test >>= 1) {
Serial.print(F("old "));
oldFunc((int)test);
Serial.print(F("new "));
newFunc((int)test);
Serial.println();
}
}
void loop() {}
``````
``````old 0000000000000001
new 1000000000000000

old 0000000000000010
new 0100000000000000

old 0000000000000100
new 0010000000000000

old 0000000000001000
new 0001000000000000

old 0000000000010000
new 0000100000000000

old 0000000000100000
new 0000010000000000

old 0000000001000000
new 0000001000000000

old 0000000010000000
new 0000000100000000

old 0000000100000000
new 0000000010000000

old 0000001000000000
new 0000000001000000

old 0000010000000000
new 0000000000100000

old 0000100000000000
new 0000000000010000

old 0001000000000000
new 0000000000001000

old 0010000000000000
new 0000000000000100

old 0100000000000000
new 0000000000000010

old 1000000000000000
new 0000000000000001
``````

Hi,

You could also code this way to print leading zeros to binary number, (example is for 16bit but it can be any length):

``````#define ZERO_PADDING 16

char binstr[]="0000000000000000";
uint8_t i=0;
uint16_t n=number;

++i;
n/=2;
}

Serial.print(binstr);
}

void setup{

uint16 n=123;

Serial.begin(115200);

Serial.Print("Integer as Decimal: ");
Serial.Prinln(n);
Serial.Print("Integer as Binary with Zero Padding: ");
}

void loop{

}
``````

Thank you for more solutions! I like the Whandall approach, it's about twice as fast (less operations there).

vito:
Thank you for more solutions! I like the Whandall approach, it’s about twice as fast (less operations there).

:o wow I agree @vito clean and fast! @whandall Thank you! I haden’t thought of doing it in that way before and I’m marketing your code part of my debug print macros.
** Karma for you! **

Z

My way :

``````byte myByte = B00110011;

void setup()
{
Serial.begin(9600);
prntBits(myByte);
}

void loop()
{

}

void prntBits(byte b)
{
for(int i = 7; i >= 0; i--)
Serial.println();
}
``````

tweaked whandall code make a more flexible function…

``````void BinaryStrZeroPad(int Number,char ZeroPadding){
//ZeroPadding = nth bit, e.g for a 16 bit number nth bit = 15

while(i>=0){
if((Number & (1<<i)) > 0) Serial.write('1');
else Serial.write('0');
--i;
}

Serial.println();
}
void setup{

uint16 n=123;

Serial.begin(115200);

Serial.Print("Integer as Decimal: ");
Serial.Prinln(n);
Serial.Print("Integer as Binary with Zero Padding: ");
}

void loop{

}
``````

tweaked whandall code make a more flexible function…

``````void BinaryStrZeroPad(int Number,char ZeroPadding){
``````

//ZeroPadding = nth bit, e.g for a 16 bit number nth bit = 15

``````while(i>=0){
if((Number & (1<<i)) > 0) Serial.write('1');
else Serial.write('0');
--i;
}
``````

Serial.println();
}
void setup{

uint16 n=123;

Serial.begin(115200);

Serial.Print("Integer as Decimal: ");
Serial.Prinln(n);
Serial.Print("Integer as Binary with Zero Padding: ");
}

void loop{

}

Numbers default as type int so your version would only work with bytes and ints not long data types. To fix this is simple:

``````if((Number & (1UL<<i)) > 0) Serial.write('1');
``````

Force the 1 to be a long. 1UL
Z

good to know.

Thanks zhomeslice

Whandall:

``````void oldFunc(int var) {
``````

for (int i = 0; i < 16; i++)  {
Serial.print(((var >> i) & 1) == 1 ? “1” : “0”);
}
Serial.println();
}

void newFunc(int var) {
for (unsigned int test = 0x8000; test; test >>= 1) {
Serial.write(var  & test ? ‘1’ : ‘0’);
}
Serial.println();
}

void setup() {
Serial.begin(250000);
for (unsigned int test = 0x8000; test; test >>= 1) {
Serial.print(F("old "));
oldFunc((int)test);
Serial.print(F("new "));
newFunc((int)test);
Serial.println();
}
}
void loop() {}

``````

``````

old 0000000000000001
new 1000000000000000

old 0000000000000010
new 0100000000000000

old 0000000000000100
new 0010000000000000

old 0000000000001000
new 0001000000000000

old 0000000000010000
new 0000100000000000

old 0000000000100000
new 0000010000000000

old 0000000001000000
new 0000001000000000

old 0000000010000000
new 0000000100000000

old 0000000100000000
new 0000000010000000

old 0000001000000000
new 0000000001000000

old 0000010000000000
new 0000000000100000

old 0000100000000000
new 0000000000010000

old 0001000000000000
new 0000000000001000

old 0010000000000000
new 0000000000000100

old 0100000000000000
new 0000000000000010

old 1000000000000000
new 0000000000000001

So cool and works a treat.

I only wanted 8 bits and a function so I could Serial.print, OLED display or FTP the binary number so I did this based on the above.

Example: Serial.println(Misc_DecTo8bitBinary(127));
or: String myBinary = Misc_DecTo8bitBinary(127);

``````String Misc_DecTo8bitBinary(int dec) {
String result = "";
for (unsigned int i = 0x80; i; i >>= 1) {
result.concat(dec  & i ? '1' : '0');
}
return result;
}
``````

Kev

bionicbone:
So cool and works a treat.

I only wanted 8 bits and a function so I could Serial.print, OLED display or FTP the binary number so I did this based on the above.

Example: Serial.println(Misc_DecTo8bitBinary(127));
or: String myBinary = Misc_DecTo8bitBinary(127);

``````String Misc_DecTo8bitBinary(int dec) {
``````

String result = “”;
for (unsigned int i = 0x80; i; i >>= 1) {
result.concat(dec  & i ? ‘1’ : ‘0’);
}
return result;
}

``````

Kev
``````

These are macros that I create that can easily handle the printing of binary numbers

``````#define PRINTBIN(Num) for (uint32_t t = (1UL<< (sizeof(Num)*8)-1); t; t >>= 1) Serial.write(Num  & t ? '1' : '0'); // Prints a binary number with leading zeros (Automatic Handling)
#define PRINTBINL(Num) for (int i=0;i<(sizeof(Num)*8);i++) Serial.write(((Num >> i) & 1) == 1 ? '1' : '0'); // Prints a binary number with following Placeholder Zeros  (Automatic Handling)
``````

usage:

``````#define PRINTBIN(Num) for (uint32_t t = (1UL<< (sizeof(Num)*8)-1); t; t >>= 1) Serial.write(Num  & t ? '1' : '0'); // Prints a binary number with leading zeros (Automatic Handling)
#define PRINTBINL(Num) for (int i=0;i<(sizeof(Num)*8);i++) Serial.write(((Num >> i) & 1) == 1 ? '1' : '0'); // Prints a binary number with following Placeholder Zeros  (Automatic Handling)

void setup() {
Serial.begin(250000);
PRINTBIN(123);
PRINTBINL(123);
}
void loop() {}
``````

Hope you like them
Z

I like 756E6C’s solution and just used it in my code - Thanks!

iamsetsetiam:
I like 756E6C's solution and just used it in my code - Thanks!

After 3 years, it might be more effective to PM them.