The question is in the title I guess.
This is the temporary solution I came up with but I was wondering:
- If there are disadvantages to representing binary as char*. Is there a better way (considering i would want the ability of bit-shifting etc…)
- If there is obvious non-idiomatic C (or other errors) in the code below.
All suggestions welcome…
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/* compile with
gcc -lm -std=c99
*/
void binary_repr(unsigned long input) {
int needed_digits = (int) (floor(log2(input)) + 1);
char *ptr_binarray = malloc((needed_digits + 1) * sizeof (char));
int idx = (needed_digits);
if (ptr_binarray == NULL) {
printf("Unable to allocate memory.");
exit(1);
}
else {
do {
idx--;
if (input % 2 == 0) {
ptr_binarray[idx] = '0';
}
else {
ptr_binarray[idx] = '1';
}
input = input / 2;
} while (input > 0);
ptr_binarray[needed_digits] = '\0';
printf("%s\n", ptr_binarray);
free(ptr_binarray);
ptr_binarray = NULL;
}
}
int main()
{
binary_repr(8);
binary_repr(14);
binary_repr(4097);
return 0;
}
Looks approximately idiomatic to me, except that I’d write the loop something like:
No need for an integer index.
For this particular case, I wouldn’t bother with
mallocsince youfreein the same function. Just allocate a big enough char array on the stack:Or make use of C99’s variable length arrays:
Also, if you’re only using gcc, then rather than taking a logarithm you could consider using
__builtin_clzto calculateneeded_digits. That’s not about idiomatic C, though, since it’s the gcc dialect. But even without it, you don’t need floating point math to figure out how many digits are needed:http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
Just noticed a probable error in that line, too – your do/while loop neatly handles the case where
inputis 0, but the first line doesn’t since you can’t take the log of 0.Is there a better way (considering i would want the ability of bit-shifting etc…)
Not sure what you mean here. If you want to do operations like bit-shifting on a value, then don’t convert it to a string like this. Keep it as a
long int, and do your bit-shifting there.Other minor things, since you’re asking for general opinions. None of these are things I’d really criticise as long as you have a reason you’ve done them:
needed_digits), it’s just noise.int idx = needed_digitsline down to just before the ‘do .. while’ loop (since you’re using std=c99. If it was c89, then you could still do that except that I’m going to recommend…).sizeof(char)in malloc, since the size of the buffer allocated by malloc is measured in chars by definition. But others put it there so that every malloc consistently always has a sizeof, so again I can’t claim my way is idiomatic. It’s just better 😉For each of the last three things, good C programming practice would not necessarily be to do as I do, but to agree a coding style with your colleagues / collaborators. The coding standard is allowed to be “do as you prefer”, just so long as you agree not to argue, and not to “tidy up” each other’s code.