Pages: [1]   Go Down
Author Topic: Serial.print(n, BIN) displaying all bits ?  (Read 1348 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way to print all bits in a value not just those from the first 1?

eg. Serial.print(B00001111, BIN); gives 1111

but it'd be helpful to show those leading zeros sometimes.

I've written a function to do it but I was wondering if there was a lower level way to do it.


Code:
char* b2s(byte myByte){
  char Str[8];
  byte mask = B10000000;
  for(int i = 0; i<8; i++){
    Str[i]='0';
    if(((mask >> i) & myByte) == (mask>>i)){
      Str[i]='1';
    }
  }
  // terminate the string with the null character
  Str[8] = '\0';
  return Str;
}

Cheers,

Andy
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps something like this:

void printBits(byte myByte){
  for(byte mask = 0x80; mask; mask >>= 1){
    if(mask  & myByte)
        Serial.print('1');
    else
        Serial.print('0');
  }
}

edit: added angle bracket missing from the for loop
« Last Edit: February 26, 2009, 06:46:25 am by mem » Logged

London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers,
So there's no core support eg. Serial.println(n, BIN8) for instance
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, the second argument to Serial.print is the number base (BIN=2) and there is no way the current core code that prints number bases knows how many places to pad – the same routine is used for one, two or 4 byte values.

BTW, I updated the code I posted above
« Last Edit: February 26, 2009, 06:57:03 am by mem » Logged

London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for confirmation and the tidy function.
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char* b2s(byte myByte) {
  char Str[8];
   :
  return Str;
}

The technique above is dangerous, as the storage for that Str array is on the stack.  The function returns, and the stack no longer "owns" that memory.  The memory is still there, and not modified yet, so sometimes the code that calls this function will work.  But calling another function after this one will damage the stack contents (setting up for different variables in the same memory).

Code:
char* b2s(byte myByte) {
  [glow]static [/glow]char Str[8];
   :
  return Str;
}

Some will say this is an improvement.  This adjustment will cause a part of memory to be dedicated to the array at all times, not just when the function is running.  So you can call the function, and trust the results are still there and safe when the function returns.

However, there is only one such array.  The next time you call the same function, the memory gets rewritten with the new values.  So your calling code needs to be aware of this, and make sure not to call the routine twice in a row without grabbing what it wanted from the results of the first call.  That's fine for your own code that you don't expect to share as a library, but I wouldn't recommend this approach for general-purpose library code.

Code:
char* b2s(byte myByte[glow], byte Str[], int length[/glow]) {
    :
  return Str;
}

This approach puts the caller in charge of array memory.  They can arrange for stack or permanent allocation, and can pass different array addresses if they want to collect multiple values without interference.  Since it's the caller's own memory space, the function doesn't even have to return the address at all, but it's still a kind of convenience thing to do.  This is the recommended technique if you want your routine to be the safest, most general-purpose, sharable routine.  
« Last Edit: February 26, 2009, 09:35:19 am by halley » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Halley, excellent advice as usual. But you left something out – the best way to prevent abusing arrays  is not to use them when they are not needed. Hence the code posted in reply #1
Logged

London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, thanks for the advice and going into that amount of detail. Explaining why is very helpful. Cheers.
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mem, exactly-- O(1) space is better than O(n), and safer to manage too.
Logged

Pages: [1]   Go Up
Jump to: