I dont really know how the whole program works. ?_?
especially the first one.
can someone tell me please.

#include<stdio.h>


int dec_bin(int bin)

                    {


                        int x, y;
                        x = y = 0;

                        for(y = 7; y >= 0; y--)
                            {
                            x = bin / (1 << y);
                            bin = bin - x * (1 << y);
                            printf("%d", x);
                            }
                            printf("\n");
                    }
                    int main ()
                          {
                    int count;
                        printf("\n\n\nConversion table - Decimal, Hexadecimal, Octal, Binary\n\n");
                        printf("Dec\tHex\tOct\tBin\n");
                        printf("----------------------------------\n");
                        for (count = 0; count <=256; count=count +1)
                            {
                            printf("%d  \t%x   \t%o\t",count, count, count);
                            dec_bin(count);
                            }

                }

The program is using something called binary shifting. For example:

1 << 1

one is binary "001" shifted by one equals binary 010, aka 2.

1 << 2

Binary 001 shifted by two is binary 100, aka 4.

So, if we assume that bin == 100 then our first iteration of the for loop would calculate:
x = bin / (1 << y);
x = 100 / (1 << 7);
x = 100 / 10000000;
x = 0; //Since x is an int the decimal portion is dropped.

bin = bin - x * (1 << y);
bin = 100 - 0 * (1 << 7);
bin = 100 - 0;
bin = 100;

Your program repeats this 7 times, but each time the shift is smaller.

If you want a simple example of converting decimal to binary you can check out Bruce Eckel's book "Thinking in C++". In chapter 3 they have a section on shift operators. You can download the book for free from:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.