Advertisement
haseeb_heaven

int_mem_bin.c

Jul 22nd, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int *binary(int);
  5. void display(int *);
  6. int check_pos(int,int);
  7. int get_val(int *,int,int);
  8.  
  9. int n,arr[8],ii=0,_bin[8],j = 0,_count=-1;
  10.  
  11. int main()
  12. {
  13.     int x, *p1, *p2, *p3, *p4;
  14.  
  15.     printf("Int Memory data:\n");
  16.     scanf("%d", &x);
  17.      
  18.     printf("4th : \t");
  19.     p4 = binary(*((char *)(&x) + 3));
  20.     display(p4);
  21.    
  22.     (x < 0) ?
  23. printf(" Value %u",get_val(p4,(j-1),4)) // for -ve numbers 2's compliment
  24.          ://:
  25. printf(" Value %d",get_val(p4,(j-1),4))
  26.          ;//;
  27. // for +ve numbers
  28.  
  29.     printf("\n3rd : \t");
  30.     p3 = binary(*((char *)(&x) + 2));
  31.     display(p3);
  32.    
  33.     printf(" Value %d",    
  34.     get_val(p3,(j-1),3));
  35.  
  36.     printf("\n2nd : \t");
  37.     p2 = binary(*((char *)(&x) + 1));
  38.     display(p2);
  39.    
  40.     printf(" Value %d",    
  41.     get_val(p2,(j-1),2));
  42.  
  43.     printf("\n1st : \t");
  44.     p1 = binary(*((char *)(&x) + 0));
  45.     display(p1);
  46.  
  47.     printf(" Value %d",
  48.     get_val(p1,(j-1),1));
  49.    
  50.     return 0;
  51. }
  52.  
  53.  
  54.  
  55. int* binary(int n)
  56. {
  57.     int c, k;
  58.  
  59.     j = 0;// reset bit -_-
  60.  
  61.     for (c = 7; c >= 0; c--)
  62.     {
  63.         k = n >> c;
  64.  
  65.         if (k & 1)
  66.             _bin[j++] = 1;
  67.  
  68.         else
  69.             _bin[j++] = 0;
  70.     }
  71.     return _bin;
  72. }
  73.  
  74.  
  75.  
  76. void display(int *p)
  77. {
  78.     int i;
  79.     for (i = 0; i < j; i++)
  80.     printf("%d", *(p + i));
  81. }
  82.  
  83.  
  84.  
  85. int check_pos(int pos, int call)
  86. {
  87.     switch (call)
  88.     {
  89.  
  90.     case 1:
  91.         return (int)pow(2, (0 + pos));
  92.         break;
  93.  
  94.     case 2:
  95.         return (int)pow(2, (8 + pos));
  96.         break;
  97.  
  98.     case 3:
  99.         return (int)pow(2, (16 + pos));
  100.         break;
  101.  
  102.     case 4:
  103.         return (int)pow(2, (24 + pos));
  104.         break;
  105.  
  106.     default : return (int)((void*)0);//its int NULL o_O
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. int get_val(int *p,int _j,int _c)
  113. {
  114.     static int _sum;
  115.    
  116.     //Reset bits -_-
  117.     _count = -1;_sum = 0;
  118.    
  119.     while (_j >= 0)
  120.     {
  121.     ++_count;// incriment _count every time
  122.    
  123.     if (p[_j--] == 1)//check in reverse order
  124.     _sum += check_pos(_count,_c);
  125.     }
  126. return _sum;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement