Advertisement
rafikamal

Clear Bit

Feb 8th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void printBinary(int n);
  4. void clearBit(int *n, int i);
  5.  
  6. int main()
  7. {
  8.     int n = 7;
  9.    
  10.     printBinary(n);
  11.     clearBit(&n, 2);
  12.     printBinary(n);
  13.    
  14.     return 0;
  15. }
  16.  
  17. void clearBit(int *n, int i)
  18. {
  19.     *n &= ~(1<<i);
  20. }
  21.  
  22. void printBinary(int n)
  23. {
  24.     unsigned int i;
  25.     for(i = 1<<31; i >= 1; i >>= 1)
  26.     {
  27.         if(i & n) printf("1");
  28.         else printf("0");
  29.     }
  30.     printf("\n");
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement