PJF16

Bitmanipulation

Nov 3rd, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define nl printf("\n")
  5.  
  6. void showBit (unsigned int);
  7. void setBit (unsigned int *, int);
  8. void clearBit (unsigned int *, int);
  9. int isSet (unsigned int a, int pos);
  10. int showBit2 (unsigned int a);
  11. void invertBit (unsigned int *a, int pos);
  12. int numberofsetBit (unsigned int a);
  13. int main (void)
  14. {
  15.     unsigned int i=3;
  16.     unsigned int pos=5;
  17.     int set=0;
  18.  
  19.  
  20.  
  21.     showBit2 (i);
  22.     nl;
  23.     setBit (&i,pos);
  24.     showBit (i);
  25.     nl;
  26.     printf ("%X",i);
  27.     clearBit (&i, pos);
  28.     nl;
  29.     showBit (i);
  30.     if (isSet (i,pos))
  31.         clearBit (&i,pos);
  32.     nl;
  33.     showBit (i);
  34.     invertBit (&i, 10);
  35.     nl;
  36.     showBit (i);
  37.     nl;
  38.     set=numberofsetBit (i);
  39.     printf ("%d",set);
  40.  
  41.     getch ();
  42.     return 0;
  43. }
  44.  
  45.  
  46.  
  47. void showBit (unsigned int a)
  48. {
  49.  
  50.     unsigned int mask=1;
  51.     int i=0;
  52.  
  53.     mask<<=sizeof(int)*8-1; //1 ganz nach links
  54.  
  55.     for (i=0; i<sizeof(int)*8; i++)
  56.     {
  57.                 // if ((a&mask)==mask)         
  58.             if (a&mask)
  59.        
  60.             printf ("%c",'1');
  61.         else
  62.             printf ("%c",'0');
  63.        
  64.         mask >>=1; //eine Stelle nach rechts
  65.     }
  66. }
  67.  
  68. void setBit (unsigned int *a, int pos)
  69.  
  70. {
  71.  
  72.     unsigned int mask=1;
  73.  
  74.     if (pos>=0 && pos<sizeof(int)*8)
  75.     {
  76.         mask=mask<<pos;
  77.         *a=*a|mask;
  78.     }
  79.  
  80.  
  81.  
  82. }
  83.  
  84. void clearBit (unsigned int *a, int pos)
  85. {
  86.  
  87.     unsigned int mask=1;
  88.  
  89.     if (pos>=0 && pos<sizeof(int)*8)
  90.     {
  91.         mask=mask<<pos; //1 auf die Position "pos" bringen
  92.         mask=~mask;
  93.         *a=*a&mask;
  94.     }
  95.  
  96.  
  97. }
  98.  
  99. int isSet (unsigned int a, int pos)
  100. {
  101.     int control=0;
  102.     unsigned int mask=1;
  103.        
  104.     if (pos>=0 && pos<sizeof(int)*8)
  105.     {
  106.         mask=mask<<pos; //1 auf die Position "pos" bringen
  107.         control=a&mask;
  108.     }
  109.  
  110.  
  111.     return control;
  112. }
  113.  
  114.  
  115. int showBit2 (unsigned int a)
  116. {
  117.     int i=sizeof(a)*8-1;
  118.  
  119.     for (;i>=0; i--)
  120.     {
  121.         if (isSet(a,i))
  122.             printf ("%c",'1');
  123.         else
  124.             printf ("%c",'0');
  125.     }
  126.  
  127.  
  128. }
  129.  
  130. void invertBit (unsigned int *a, int pos)
  131. {
  132.     unsigned int mask=1;
  133.  
  134.     if (pos>=0 && pos<sizeof(int)*8)
  135.     {
  136.         mask=mask<<pos;
  137.         *a=*a^mask;
  138.  
  139.  
  140.     }
  141.  
  142.  
  143.  
  144. }
  145.  
  146. int numberofsetBit (unsigned int a)
  147. {
  148.     unsigned int mask=1;
  149.     int z=0;
  150.     int pos=0;
  151.    
  152.     do
  153.     {
  154.         if(isSet (a,pos))
  155.             z++;
  156.  
  157.         pos++;
  158.  
  159.     }while (pos<sizeof(int)*8);
  160.         return z;
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment