integer leading zeros

hy,

i'm writing this program where i need to pas a binary form long int variable to a function and then do some processing with that variable.

so it would be something like this :

my var is : long int myVar = 0b0010 for ex

and what gets passed on to my func is myVar = 0b10, arduino just trims the leading zeros, which in this case are important.

any thoughts on how to solve this problem and pass on the right value to my function? that is : myVar = 0b0010 if we are to keep my example.

PS: this is to be a binary buffer used to send to a bus.

well any help is extremly apreciated XD tnx

Only in strings (e.g. for printing) leading zero's make sense. For a computer 0b0010 == 0b10

You can process the leading zero by using bitRead() like in the sketch below: you can use less leading zero's of course...

void f(uint32_t l)  // shorter than unsigned long :)
{
  for (int i=31; i>=0; i--)
  {
    Serial.print(bitRead(l, i), DEC);
  }
  Serial.println();
}

void setup()
{
  Serial.begin(115200);   
  f(12653434L);
  f(3);
}

void loop(){}

first off thanks for the reply mate.

and actually i gotta send a buffer to a serial bus :

unsigned long adress = 0b00010000010000000011;

it’s a 8b/10b encoding so it would be something like this 0001 0000 01 0000 0000 11 (1000) which is something else entirely without the leading zeros : 1000 0010 00 0000 011 (it would make 800 and an incomplete frame for the 011). but i kinda wrote a patch for it, well more of a hack actualy : i passed the variable to a string and then used it. i think it’s the way they wanted us to pass variables with leading zeros as 023 for example is 23 in base 8 math. So anyway, being to late to make along story short, here’s the code :

String adress = "00010000010000000011";  //0001 0000 01 0000 0000 11
int pin = 3;


void setup() {                
  pinMode(pin, OUTPUT);    
  Serial.begin(9600); 
}


//==============================================================================

void low(){

  Serial.print("0");
  digitalWrite(pin,HIGH);
  delay(1.5);
  digitalWrite(pin,LOW);
  delay(0.9); 
    
}


//==============================================================================

void high(){
  
  Serial.print("1");    
  digitalWrite(pin,HIGH);
  delay(0.6); 
  digitalWrite(pin,LOW);
  delay(1.8); 
  
}

//==============================================================================

void loop () {
  
   for (int i = 0; i<adress.length();i++) {
    
    if (i==4 or i==8 or i%10==0 or i==14 or i==18){
        Serial.print(" "); 
      }
      
    if (adress[i] == '1') 
          high();
    else low();
  }
  Serial.println();
  
}

there are a few points to consider:

  • Serial print statements for debugging take time. @9600 baud one character takes approx 1 milliSecond => remove them from sending the pulses
  • string is printed separately
  • delay(0.6) will not work as delay expects an integer. use delayMicroseconds(600) instead

tinkered your code:

String adress = "00010000010000000011";  //0001 0000 01 0000 0000 11
int pin = 3;

void low()
{
  digitalWrite(pin,HIGH);
  delayMicroseconds(1500);
  digitalWrite(pin,LOW);
  delayMicroseconds(900);   
}

void high()
{
  digitalWrite(pin,HIGH);
  delayMicroseconds(600); 
  digitalWrite(pin,LOW);
  delayMicroseconds(1800);
}

//==============================================================================
void setup() 
{                
  pinMode(pin, OUTPUT);    
  Serial.begin(115200);    // Max Speed to minimize the impact of the debug state
}
void loop () 
{
  // PRINT WITHOUT INTERFERENCE
  for (int i = 0; i<adress.length();i++) 
  {
    Serial.print(adress[i]);
    if (i==4 or  i==8 or i==10 or i==14 or i==18) Serial.print(" ");   //removed the % calculation as it is very expensive...
  }
  Serial.println();

  // NOW CLOCK THE BITS
   for (int i = 0; i<adress.length();i++) 
   {
      if (adress[i] == '1') high();
      else low();
  }
  // 
}