Ok..I get how to do binary division and have written this simple program (everything except the output with putchar()). My goal, however, is to invert my input and print it out. I ran into a few problems trying to accomplish this:
- How do I get the one's complement operator ~ to work? I've tried inserting it here and there but all i get is smiley faces or some funky symbols.
- In putchar, what does '0' + r do?
/*
1. Get bits of unsigned char x and store into y.
2. Invert.
*/
#include <stdio.h>
int to_binary(int);
void main(void)
{
unsigned char x, y;
puts("Enter a value for x:");
scanf("%d", &x);
y = to_binary(x);
}
int to_binary(int n)
{
int r;
r = n % 2;
if (n >= 2)
to_binary(n / 2);
putchar('0' + r);
return r;
}