# Forcing a 16 digit display for a 16 bit binary

I am looking for a way of forcing a 16 bit binary to be displayed as 16 digits even if bit 16 is a 0 and not displayed.

Problem: I am summing up some integers being converted to a binary to control the output pins of 2 74HC595 shift registers. I would like to use the same binary to be displayed on a LCD as status report to see which pin is supposed to be HIGH (1) or LOW (0).

using

``````lcd.print(value, BIN);
``````

works fin if bit 16 comes out as HIGH (1). Soon as it comes out as 0 the display shifts 1 digit to the left. If bit 16 and 15 come out as LOW the displayed binary shifts 2 digits to the left.

Is there a way of forcing the binary to always display the 16 digits or convince the LCD to display from right to left and not from left to right as normal?

Thanks for having a look again.

i am way out of my league here, so please take this with a pint of salt...

8)

I remember once reading somewhere that numbers starting with a 0 should be avoided when writing a number for an Arduino. It had something to do with the number being unintentionally assumed as an Octal number (because they are indicated with the prefix "0").

Maybe worth taking a look...

EDIT:
i was now looking this up, and on the Arduino IntegerConstant reference page, when talking about the Octal Numbers there is the following warning:

"It is possible to generate a hard-to-find bug by (unintentionally) including a leading zero before a constant and having the compiler unintentionally interpret your constant as octal."

You can check it out here:

The only way I know how to do that is to convert the number into a string and then if necessary, add the leading zeros to the string with additional code. Here is a start in standard C using printf:

``````  char buffer [33];
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
``````

Thanks, but that's not the problem since I am currently using the internal conversion (value, BIN) to display a integer as binary.

The problem is that I would need for displaying purposes the 16 bit binary to keep the 16 digits. Since binaries drop there last digit if it turns out to be a 0.

I could determine another way if the status is 1 or 0 and print it out with a heap of else if statements like

``````if(switchState[1] == 1){
lcd.setCursor(15, 0);
lcd.print("1");
}
else {
lcd.setCursor(15, 0);
lcd.print("0");
}
``````

and carry on for 15 more switch states.

I think it would be shorter to print the 16 bit binary if it can be forced to keep the 16 digits or convince the LCD to start displaying from left to right.

and print it out with a heap of else if statements like

Or, more sensibly, a single if/else and a for loop.

boguz:
I remember once reading somewhere that numbers starting with a 0 should be avoided when writing a number for an Arduino. It had something to do with the number being unintentionally assumed as an Octal number (because they are indicated with the prefix “0”).

That’s true, but only when you are writing a program, and putting in a constant value. You are quite safe writing strings with leading zeros to an LCD or Serial.

``````void setup() {
Serial.begin(115200);
byte x = 20;
Serial.println(x);
x = 020;
Serial.println(x);
}

void loop() {
}
``````

This code will output 20, then 16.

lar3ry:
That's true, but only when you are writing a program, and putting in a constant value. You are quite safe writing strings with leading zeros to an LCD or Serial.

Thanks

Rather then printing the value to a string, and then sending the string to the LCD, you can use bitRead() to read the nth bit, and then send ‘0’ or ‘1’ to the LCD, depending on what you read. Or send ‘0’ plus what you read.

Thanks a lot for all the help.

I think I have to go with bitRead().

just to add an example for the 'for if' thing:

``````void printBinary16(unsigned int iIn)  {
//                       0b1234567812345678