How can I; Serial.println() a binary output that will give all the leading zero's of a variable?

Does someone know how to get all the leading zeros to print when a variable is sent to the console?

The output I would like to see on the console should be something like this
for example: when bits 0,3 & 4 are set -
--> 00011001

Thanks in advance.

  unsigned long myTime = 0; 
  byte x = 0b00000000;
  
void setup() {
  Serial.begin(9600);
  Serial.println((String)"    system Timer = " + millis());
  //Timing values: ~979/sec 
  myTime = millis() + 14685;   //this should be about 15seconds
  Serial.println((String)"    Timer Value = " + myTime);
}

void loop() {
  if (millis() >= myTime) {
    myTime = millis() + 14685;
  Serial.println((String)"    Timer Value = " + myTime);
  // Biting through the flags
   Serial.println(x, BIN);
  
   if (bitRead(x, 0) == false) {
                                Serial.println((String)"1st Bit Value = " + x);
                                bitWrite(x,0,1);
                               } else {
                                if (bitRead(x, 1) == false) {
                                Serial.println((String)"2nd Bit Value = " + x);
                                bitWrite(x,1,1);
                                } else {
                                  if (bitRead(x, 2) == false) {
                                Serial.println((String)"3rd Bit Value = " + x);
                                bitWrite(x,2,1);
                                } else {
                                  if (bitRead(x, 3) == false) {
                                Serial.println((String)"4th Bit Value = " + x);
                                bitWrite(x,3,1);
                                  
                                } else {
                                  if (bitRead(x, 4) == false) {
                                Serial.println((String)"5th Bit Value = " + x);
                                bitWrite(x,4,1);
                                
                                } else {
                                  if (bitRead(x, 5) == false) {
                                Serial.println((String)"6th Bit Value = " + x);
                                bitWrite(x,5,1);
                                  
                                } else {
                                  if (bitRead(x, 6) == false) {
                                Serial.println((String)"7th Bit Value = " + x);
                                bitWrite(x,6,1);
                                  
                                } else {
                                  if (bitRead(x, 7) == false) {
                                Serial.println((String)"8th Bit Value = " + x);
                                bitWrite(x,7,1); 
                                
                                }

                                }}}}}}}
   }   
}                    
1 Like

try this

void printBin(byte aByte) {
  for (int8_t aBit = 7; aBit >= 0; aBit--)
    Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}
void setup() {
  Serial.begin(115200); Serial.println();
  byte x = 0b00001100;
  printBin(x);
}

void loop() {}
2 Likes

@J-M-L
DUDE!!! that works perfectly. its exactly what I needed. Thanks again!

cool have fun :slight_smile:

I woud prefer the following version:

Serial.print(bitRead(aByte, aBit) ? '1' : '0');

Becasue '1' and '0' are (visibly) more friendly with print() method.

why would you call print() for a char ? it does not make sense at all...(print() will call write() so you are adding one indirection and some bytes on the stack for nothing... do you like slow code :slight_smile: ??)

see here:

Then why not the following style:

Serial.write(bitRead(aByte, aBit) ? 0x31 : 0x30);

when the argument of write() method demands this: " val : a value to send as a single byte"?

because it's WAY more readable as '0' and '1'. Those are perfect single byte values according to the spec and you don't need to go check an ASCII table... I let the compiler do the work..

I don't get your point

Serial.write(val) method demands that the argument (val) should have a single byte data (8-bit). The '1' and '0' are not visibly 8-bit data though they are are converted into 8-bit data (0x31 and 0x30) at the time of transmission.

why are they not visibly single bytes? they are well defined character literal

And it's actually when you explicitly write 0x31 that you don't have a byte... This is treated as an int so will look like 2 or 4 bytes initially (before the compiler does the right thing)

Saying visible -- I have wanted to mean this: what I see when I look upon.

Serial.write('1') and Serial.Write(0x31) do the same job; but, I don't see an 8-bit value in the argument field of this Serial.write('1') command.

OK you don't and I do :slight_smile:

And I do see an int when you write 0x31, not a byte...

In my computer, the sizeof(int) shows 4 and accordingly it is Serial.write(0x00000031) and not Serial.write(0x31). I hope that I have got your point. :smile:

(However, the kids always refer to the Arduino Reference Manual; where, the following is stated: Serial.write(val) ; val : a value to send as a single byte.)

you did :slight_smile:

(from my understanding you are no longer a kid :slight_smile: )

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.