home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / allison / bit1_2.c < prev    next >
C/C++ Source or Header  |  1993-09-01  |  764b  |  67 lines

  1. LISTING 2 - Sets and Resets Individual Bits */
  2. /* bit1.c:  Set and reset individual bits */
  3.  
  4. #include <stdio.h>
  5. #include <limits.h>
  6.  
  7. #define WORD     unsigned int
  8. #define NBYTES   sizeof(WORD)
  9. #define NBITS    (NBYTES * CHAR_BIT)
  10. #define NXDIGITS (NBYTES * 2)
  11.  
  12. main()
  13. {
  14.     unsigned n = 0;
  15.     int i;
  16.  
  17.     /* Set each bit in turn */
  18.     for (i = 0; i < NBITS; ++i)
  19.     {
  20.         n |= (1u << i);
  21.         printf("%0*X\n",NXDIGITS,n);
  22.     }
  23.  
  24.     /* Now turn them off */
  25.     for (i = 0; i < NBITS; ++i)
  26.     {
  27.         n &= ~(1u << i);
  28.         printf("%0*X\n",NXDIGITS,n);
  29.     }
  30.     return 0;
  31. }
  32.  
  33. /* Output
  34. 0001
  35. 0003
  36. 0007
  37. 000F
  38. 001F
  39. 003F
  40. 007F
  41. 00FF
  42. 01FF
  43. 03FF
  44. 07FF
  45. 0FFF
  46. 1FFF
  47. 3FFF
  48. 7FFF
  49. FFFF
  50. FFFE
  51. FFFC
  52. FFF8
  53. FFF0
  54. FFE0
  55. FFC0
  56. FF80
  57. FF00
  58. FE00
  59. FC00
  60. F800
  61. F000
  62. E000
  63. C000
  64. 8000
  65. 0000
  66. */
  67.