[1+1=2]
OneAndOneIs2

« First Google, and now MicrosoftComing to a grinding halt »

Sun, Jan 21, 2007

[Icon][Icon]Back into binary

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;
}

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)

Blogroll

iconLittleRedBoat
[Link]I am not boasting in the slightest, I promise
iconSmallerThanLife
[Link]Venn Will I Be Famous?
iconI am livid
[Link]Somalian Pirates
iconCreative Hedgehog
[Link]on books and reading
iconHari's corner
[Link]Brand new blogging software
iconPlace of Stuff
[Link]An Enforced Break
iconNik Butler
[Link]You should Fire your Web agency when ?
iconAdvice From a Single Girl
[Link]Learned the Hard Way
iconA Toast To Shoes
[Link]

Blogroll generated by MagpieRSS

[Icon]My links

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My facebook profile

[Icon][Icon]My last.fm profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]
November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

blog soft