Test
2017-07-21 20:52:28 UTC
I got a function that converts an unsigned int to binary string of ones and
zeroes:
void int_to_bin_digit(unsigned int in, int count, char * out)
{
unsigned int mask = 1U << (count-1);
int i;
for (i = 0; i < count; i++)
{
out[i] = (in & mask) ? '1' : '0';
in <<= 1;
}
}
for example if I do:
int_to_bin_digit(4157704578,32,myout);
.."myout" should have "11110111110100011000100110000010" (no zero at the end).
It works fine on compiled wuth gcc. I am converting it to work on an Arduino
where "int" is 16 bit wide - not 32.
I tried to used unsigned long (which should be 32 bits wide on Arduino compiler:
void int_to_bin_digit(unsigned long in, int count, char * out)
{
unsigned long mask = 1U << (count-1); //problem probably here
int i;
for (i = 0; i < count; i++)
{
out[i] = (in & mask) ? '1' : '0';
in <<= 1;
}
}
Unfortunately I get just "00000000000000000000000000000000". The reason is that
mask gets zero and not 2147483648 as in the gcc version.
What should I change?
---
This email has been checked for viruses by AVG.
http://www.avg.com
zeroes:
void int_to_bin_digit(unsigned int in, int count, char * out)
{
unsigned int mask = 1U << (count-1);
int i;
for (i = 0; i < count; i++)
{
out[i] = (in & mask) ? '1' : '0';
in <<= 1;
}
}
for example if I do:
int_to_bin_digit(4157704578,32,myout);
.."myout" should have "11110111110100011000100110000010" (no zero at the end).
It works fine on compiled wuth gcc. I am converting it to work on an Arduino
where "int" is 16 bit wide - not 32.
I tried to used unsigned long (which should be 32 bits wide on Arduino compiler:
void int_to_bin_digit(unsigned long in, int count, char * out)
{
unsigned long mask = 1U << (count-1); //problem probably here
int i;
for (i = 0; i < count; i++)
{
out[i] = (in & mask) ? '1' : '0';
in <<= 1;
}
}
Unfortunately I get just "00000000000000000000000000000000". The reason is that
mask gets zero and not 2147483648 as in the gcc version.
What should I change?
---
This email has been checked for viruses by AVG.
http://www.avg.com