////////////////////////////////////////////////////////////////////////////////
//                        Coypright (C) Aviral Mittal.
////////////////////////////////////////////////////////////////////////////////
//   All rights reserved. Reproducion in whole or in part is prohibited without 
//   written consent of copyright Owner.The professional use of this material 
//   is subject to the copy right owners approval in written. 
////////////////////////////////////////////////////////////////////////////////
//                   Comments welcome on avimit@yahoo.com
//                   Visit http://www.vlsiip.com for more free stuff
////////////////////////////////////////////////////////////////////////////////
// This program Intakes an integer, and produces its 32 bit binary eq number
// Usage:
// Unix> ./a.out 255
// 0000_0000_0000_0000_0000_0000_1111_1111
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>

unsigned long int myint,myint1;
char i,len;
char mybin[32];
void char2bin(unsigned long x,char xx[]);
void char2bin1(unsigned long x,char xx[]);
long int  po(short int x,short int y);
char mystr[32];
char *pmystr;
int main(int argc, char* argv[])
{
////////////////////////////////////////////////////////////////////////////////
//INITIALIZATION
////////////////////////////////////////////////////////////////////////////////
          pmystr = &mystr[0];
          i = 0;
          myint = 0;
          myint1 = 0;
          len = 0;
        if(argc == 2) { //check if the user has given any inputs to the program
          while(*argv[1] != '\0') {
            *pmystr = *argv[1];
            argv[1]++;
            pmystr++;
            len++;
          }
          *pmystr = '\0';
          //printf("mystr = %s\n",mystr);
          pmystr = &mystr[0];
          while(*pmystr != '\0') {
            myint = (unsigned long int) (*pmystr-48)*po(10,(len-i-1));
            //printf("myint = %d\n",myint);
            myint1 = myint1 + myint;
            pmystr++;
            i++;
          }
          //printf("len = %d\n",len);
          //printf("Input Int =  %d \n",myint1);

          //printf("Integer %d is equal to binary \n",myint1);
          char2bin(myint1,mybin);
          printf("\n");
        }//if(argc == 2)
        else {
          printf("########################################################\n");
          printf("Error: Incorrect Input Arguments, One Expected\n");
          printf("This program converts an Integer number into Binary \n");
          printf("\n");
          printf("Example: To Convert 255 Into Binary \n");
          printf("Unix> calbin_ext 255 \n");
          printf("0000_0000_0000_0000_0000_0000_1111_1111\n");
          printf("\n");
          printf("########################################################\n");
          printf("\n");
        } //else of if(argc == 2)
}//main

void char2bin(unsigned long x,char xx[])
{
  int j;

  for(j=0;j<32;j++) {
    xx[31-j] = ((x >> (31 - j)) & 1);
    if(j==4 || j==8 || j==12 || j==16 || j==20 || j==24 || j==28) { 
      printf("_");
    }
    printf("%d",xx[31-j]);
  }
}
void char2bin1(unsigned long x,char xx[])
{
  int j;

  for(j=0;j<32;j++) {
    xx[31-j] = ((x >> (31 - j)) & 1);
    printf("%d",xx[31-j]);
  }
}
long int  po(short int x,short int y)
{
  int j;
  long int ret=1;
  for(j=0;j<y;j++) {
    ret = ret*x;
  }
  return(ret);
}

