rajeevs1992

bits.c

Dec 24th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE(n) (n+8-(n%8))/8
  4. #define PRESENT 1
  5. #define ABSENT 0
  6. #define POS(n) n/8
  7. struct number
  8. {
  9.     unsigned char num0:1;
  10.     unsigned char num1:1;
  11.     unsigned char num2:1;
  12.     unsigned char num3:1;
  13.     unsigned char num4:1;
  14.     unsigned char num5:1;
  15.     unsigned char num6:1;
  16.     unsigned char num7:1;
  17. };
  18. void set(int num,struct number list[]);
  19. void display(int limit,struct number list[]);
  20. void SetToZero(int limit,struct number list[]);
  21. main()
  22. {
  23.     int num=0,upper;
  24.     printf("\nEnter largest number in list ");
  25.     scanf("%d",&upper);
  26.     upper+=1;
  27.     struct number list[SIZE(upper)];
  28.     printf("%d",sizeof list);
  29.     SetToZero(SIZE(upper),list);
  30.     printf("Enter the list  ");
  31.     scanf("%d",&num);
  32.     while(num<upper)
  33.     {
  34.         set(num,list);
  35.         scanf("%d",&num);
  36.     }
  37.     display(SIZE(upper),list);
  38. }
  39. void SetToZero(int limit,struct number list[])
  40. {
  41. //Initialized the whole list with 0
  42.     int i;
  43.     for(i=0;i<limit;i++)
  44.     {
  45.         list[i].num0=ABSENT;
  46.         list[i].num1=ABSENT;
  47.         list[i].num2=ABSENT;
  48.         list[i].num3=ABSENT;
  49.         list[i].num4=ABSENT;
  50.         list[i].num5=ABSENT;
  51.         list[i].num6=ABSENT;
  52.         list[i].num7=ABSENT;
  53.     }
  54. }
  55. void set(int num,struct number list[])
  56. {
  57. //Function sets the recieved number on to the list
  58.     switch(num%8)
  59.     {
  60.         case 0:
  61.             list[POS(num)].num0=PRESENT;
  62.             break;
  63.         case 1:
  64.             list[POS(num)].num1=PRESENT;
  65.             break;
  66.         case 2:
  67.             list[POS(num)].num2=PRESENT;
  68.             break;
  69.         case 3:
  70.             list[POS(num)].num3=PRESENT;
  71.             break;
  72.         case 4:
  73.             list[POS(num)].num4=PRESENT;
  74.             break;
  75.         case 5:
  76.             list[POS(num)].num5=PRESENT;
  77.             break;
  78.         case 6:
  79.             list[POS(num)].num6=PRESENT;
  80.             break;
  81.         case 7:
  82.             list[POS(num)].num7=PRESENT;
  83.             break;
  84.     }
  85. }
  86. void display(int limit,struct number list[])
  87. {
  88. //Displays the list,automatically sorted
  89.     int i=0;
  90.     for(i=0;i<limit;i++)
  91.     {
  92.         if(list[i].num0)
  93.             printf("%d\t",i*8);
  94.         if(list[i].num1)
  95.             printf("%d\t",i*8+1);
  96.         if(list[i].num2)
  97.             printf("%d\t",i*8+2);
  98.         if(list[i].num3)
  99.             printf("%d\t",i*8+3);
  100.         if(list[i].num4)
  101.             printf("%d\t",i*8+4);
  102.         if(list[i].num5)
  103.             printf("%d\t",i*8+5);
  104.         if(list[i].num6)
  105.             printf("%d\t",i*8+6);
  106.         if(list[i].num7)
  107.             printf("%d\t",i*8+7);
  108.     }
  109.     printf("\n");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment