1. ## binary questions

Hi,

At http://www.cs.cf.ac.uk/Dave/C I am studying C and I also (try to) make the exercises. I can't solve this one: http://www.cs.cf.ac.uk/Dave/C/node13.html#ex:bin

Exercise 12507

Write a function that prints out an 8-bit (unsigned char) number in binary format.
I tried something:

Code:
```#include <stdio.h>

int main(int argc, char **argv)
{
unsigned char x;
x = 89;

printf("%d", x);

return 0;
}```
Actually, I don't have a clue how to do this

2. C doesn't have a way of printing binary values. You have to do some conversions first. If you want to do it manually, you'll end up using bitwise operators to separate the value into bits so that you can print them individually.

3. %b for binary formatting in printf was deliberately left out so that students would have an easy homework question to solve

4. After some "googling" I found this site and coded the solution:

Code:
```#include <stdio.h>

int main(int argc, char **argv)
{
unsigned char x; // 8 bits
x = 69;

int count;
for(count = 8; count > 0; --count)
{
printf("%d", (x & 128) ? 1 : 0);
x <<= 1;
}

return 0;
}```
And after some experimenting I now finally understand "binary stuff" much better

5. It is no surprise I like Ideswa . He likes binary,uses gcc and ubuntu !
I suppose I'm just biased that way.

6. Originally Posted by Salem
%b for binary formatting in printf was deliberately left out so that students would have an easy homework question to solve
Then was the creat() function designed just to show CS students that a lot of things in CS don't make sense, like writing the word 'create' without the 'e'.

Har har har

7. They had already used create for other things, and leet speak hadn't caught on yet, so they couldn't use creat3.

8. I used to think &#37;b would have been a good format specifier for printf() and scanf(). Now I seem to think it less necessary, but it still will would have been nice.

9. Originally Posted by MacGyver
I used to think %b would have been a good format specifier for printf() and scanf(). Now I seem to think it less necessary, but it still will would have been nice.
Honestly though, I've NEVER had a need to display a value in binary, outside of some school assignment. The close relationship between binary and hex almost always makes hex a better choice anyway.

10. I agree with the hex-binary relationship. Once I got used to hex, I decided it made a lot more sense to use than straight binary, especially for larger binary values (32-bit and now 64-bit).

Even with that said, I still think it's odd that we have decimal, octal, hexadecimal, and yet no binary. Let's just throw away octal and put binary in its place.

/me waits for a bunch of angry octal fans to throw a fit.

11. Originally Posted by honorable_sir
It is no surprise I like Ideswa . He likes binary,uses gcc and ubuntu !
I suppose I'm just biased that way.
Yes, he's misguided too.