home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / vcron.t.Z / vcron.t / VCRON / bitstring.h next >
Text File  |  1988-11-15  |  3KB  |  124 lines

  1. /* bitstring.h - bit string manipulation macros
  2.  * vix 26feb87 [written]
  3.  * vix 03mar87 [fixed stupid bug in setall/clearall]
  4.  * vix 25mar87 [last-minute cleanup before mod.sources gets it]
  5.  */
  6.  
  7. #ifndef    _bitstring_defined
  8. #define    _bitstring_defined
  9.  
  10. /*
  11.  * there is something like this in 4.3, but that's licensed source code that
  12.  * I'd rather not depend on, so I'll reinvent the wheel (incompatibly).
  13.  */
  14.  
  15. /*
  16.  * except for the number of bits per int, and the other constants, this should
  17.  * port painlessly just about anywhere.  please #ifdef any changes with your
  18.  * compiler-induced constants (check the CC man page, it'll be something like
  19.  * 'vax' or 'mc68000' or 'sun' or some such).  also please mail any changes
  20.  * back to me (ucbvax!dual!ptsfa!vixie!paul) for inclusion in future releases.
  21.  */
  22.  
  23. /*
  24.  * (constants used internally -- these can change from machine to machine)
  25.  */
  26.             /*
  27.              * how many bits in the unit returned by sizeof ?
  28.              */
  29. #define    _bit_charsize    8
  30.  
  31.             /*
  32.              * what type will the bitstring be an array of ?
  33.              */
  34. #define    _bit_type    unsigned int
  35.  
  36.             /*
  37.              * how many bits in an int ?
  38.              */
  39. #define    _bit_intsiz    (sizeof(_bit_type) * _bit_charsize)
  40.  
  41.             /*
  42.              * an int of all '0' bits
  43.              */
  44. #define    _bit_0s        ((_bit_type)0)
  45.  
  46.             /*
  47.              * an int of all '1' bits
  48.              */
  49. #define    _bit_1s        ((_bit_type)~0)
  50.  
  51. /*
  52.  * (macros used internally)
  53.  */
  54.     /*
  55.      * how many int's in a string of N bits?
  56.      */
  57. #define    _bit_size(N) \
  58.     ((N / _bit_intsiz) + ((N % _bit_intsiz) ? 1 : 0))
  59.  
  60.     /*
  61.      * which int of the string is bit N in?
  62.      */
  63. #define    _bit_intn(N) \
  64.     ((N) / _bit_intsiz)
  65.  
  66.     /*
  67.      * mask for bit N in it's int
  68.      */
  69. #define    _bit_mask(N) \
  70.     (1 << ((N) % _bit_intsiz))
  71.  
  72. /*
  73.  * (macros used externally)
  74.  */
  75.     /*
  76.      * declare (create) Name as a string of N bits
  77.      */
  78. #define    bit_decl(Name, N) \
  79.     _bit_type Name[_bit_size(N)];
  80.  
  81.     /*
  82.      * declare (reference) Name as a bit string
  83.      */
  84. #define    bit_ref(Name) \
  85.     _bit_type Name[];
  86.  
  87.     /*
  88.      * is bit N of string Name set?
  89.      */
  90. #define    bit_test(Name, N) \
  91.     ((Name)[_bit_intn(N)] & _bit_mask(N))
  92.  
  93.     /*
  94.      * set bit N of string Name
  95.      */
  96. #define    bit_set(Name, N) \
  97.     { (Name)[_bit_intn(N)] |= _bit_mask(N); }
  98.  
  99.     /*
  100.      * clear bit N of string Name
  101.      */
  102. #define    bit_clear(Name, N) \
  103.     { (Name)[_bit_intn(N)] &= ~_bit_mask(N); }
  104.  
  105.     /*
  106.      * set bits 0..N in string Name
  107.      */
  108. #define    bit_setall(Name, N) \
  109.     {    register _bit_i; \
  110.         for (_bit_i = _bit_size(N)-1; _bit_i >= 0; _bit_i--) \
  111.             Name[_bit_i]=_bit_1s; \
  112.     }
  113.  
  114.     /*
  115.      * clear bits 0..N in string Name
  116.      */
  117. #define    bit_clearall(Name, N) \
  118.     {    register _bit_i; \
  119.         for (_bit_i = _bit_size(N)-1; _bit_i >= 0; _bit_i--) \
  120.             Name[_bit_i]=_bit_0s; \
  121.     }
  122.  
  123. #endif    _bitstring_defined
  124.