|
| « First Google, and now Microsoft | Coming to a grinding halt » |
Sun, Jan 21, 2007
![[Icon]](rsc/img/chain_link.gif)
I'd actually forgotten just how powerful and useful binary could be as a base: Sure, it's good for computers that are too dumb to use numbers bigger than "one" but for some types of math, it's really convenient.
Such as doubling a number: If you were asked to double 5983470658721 you'd probably have to scratch your head for a few moments and work it out longhand.
If you were asked to double the binary number 1010101010111010, you'd just add a zero to the end and you'd be done. Multiplying by eight? Add three zeros and you're done.
Clever. . .
So I wanted to generate a list of binary numbers so I could have a play. That meant I had to look up the math that converts from decimal to binary, and then implement it in C. This I have now done.
I probably should have broken it into more than one function, but it didn't seem worth it for so simple a task. And I don't like having to define the width of the binary results: I'm sure there's a way to make C calculate this for itself using a macro in the #define, but sadly I haven't learned how to do this yet, so it has to be done manually.
Ah well, I can always come back to it when I get far enough into K&R.
Follow up:
#include <stdio.h>
/* MAXNUM defines how many numbers you want to calculate binary values for */
#define MAXNUM 20
/* For neatness, all binary values should be the same width, defined here
* MAKE SURE you define a large enough number to fit all calculated values in */
#define BINWIDTH 6
int main ()
{
int i, j, c, d;
char bin[BINWIDTH];
for (i = 1; i <= MAXNUM; i++)
{
/* First, print our current value, justified to 5 digits here */
printf("%5d = ", i);
j = i;
c = 0;
/* Binaries are calculated by the remainder of dividing by two,
* then dividing the integer by two, and repeating:
* Hence thirteen, for example:
* 13 / 2 = 6 remainder 1
* 06 / 2 = 3 remainder 0
* 03 / 2 = 1 remainder 1
* 01 / 2 = 0 remainder 1
* Thus the binary value for 13 is 1101 */
while (j > 0)
{
/* This will put the binary value into the array bin[]
* in REVERSE ORDER (See above) - this is dealt with at
* a later stage in the program.
*
* "bin[c++]" populates the value of 'c' then
* increments c by one afterwards.
*
* "j%2" finds if the binary value should be 1 or 0
*
* "+'0'" turns the number into an ASCII character */
bin[c++] = ((j % 2) +'0');
j /= 2;
}
/* Before we print out the obtained binary value, we must print
* the necessary number of blank zeros first */
d = c;
while (d++ < BINWIDTH)
putchar('0');
/* The zeros now printed, we can print our binary value,
* starting at the end and working back to the start. Because
* we incremented 'c' once too many times previously, we can
* use c = 0 as our endpoint if we use --c rather than c in
* putchar */
while (c > 0)
putchar(bin[--c]);
putchar('\n');
}
return 0;
}
LittleRedBoat
I am not boasting in the slightest, I promise
Venn Will I Be Famous?
Somalian Pirates
on books and reading
Brand new blogging software
An Enforced Break
You should Fire your Web agency when ?
Learned the Hard Way![[Link]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)