Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

modifying arrays in C

backstory (if interested):

Recently, I have learned to write numbers in binary. So, I wanted to see if I could write every single possibility for 32-bits. So, I started, and after having successfully finished a mere 10-bits, I looked up the total amount of solutions for 32-bits. Turns out it is way way way more than I thought (well over 4 billion). So I thought a fun challenge would be to write a custom program (in C) that would calculate every single possible answer for 32-bits.

For this, I used an array with 32 items in it. Then, I check if the 32nd value is equal to 0. If it is, then I simply change it to be 1. If it is 1, I change it to be 0, and then change every item after that (equal to 1) back to zero, until I hit a 0.
Here is my code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdlib.h>

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int bits[32] = {1, 0};
    
    for (int i=0;i<3000;i++)
    {

        for (int j=0;i<32;i++)
        {
            if (bits[32] == 0)
            {
                bits[32] = 1;
            }
            else if (bits[32] == 1)
            {   
                int k = 0;
                
                for (k = 0;k<32;k++)
                {
                    int position = 32-k;
                    while(bits[position] != 0)
                    {
                        bits[position] = 0;
                    }
                    bits[position+1] = 1;
                }
            }
        }
        for (int i=0;i<32;i++)
        {
            printf("%d", bits[i]);
        }
        printf("\n");
    }
    getchar();
    return 0;
}

And then I hit a major roadblock. It didn’t work at all (just plug into any IDE, run it and you will see it for yourself). It seemed perfectly logical in my head, but apparently it doesn’t work like that, and I will admit I am completely lost.
Does anyone know (without feeding me the whole code, I still want to do it myself) why this doesn’t work, and how to fix it ?
Thank you very much.

>Solution :

Well, what you asked for is already in the comments but sill if you want to check wether what you coded yelds the correct result, here is a way simpler solution.

Keep in mind that a long is also a 32 bit variable (or 64 on x64). All you have to do is to print each bit in the variable. Try this:

#include <stdio.h>
void PrintBin(unsigned long uNumber)
{
    for(unsigned long i = 0; i < 32; i++)
        printf("%lu", (uNumber & (0x80000000 >> i)) >> (31-i));
    printf("\n");
}

int main()
{
    unsigned long i = 0;
    do
    {
        printf("%lu --- ", i);
        PrintBin(i);
        i++;
    }while(i != 0);

    return 0;
}

All numbers are already in binary, you just have to print them. Well ofc if we are not talking about reading the number as a string and then convert it. Thats another story.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading