File: bitops.h

package info (click to toggle)
ns2 2.35%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 78,756 kB
  • ctags: 27,476
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 812; awk: 525; csh: 355
file content (41 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef BITOPS_H
#define BITOPS_H

#include "string.h"      /* due to memset */



/* determines if bit number "bit_nb" is set in array "arr" */
#define IS_BIT_SET(arr, bit_nb) (((unsigned char*) arr)[(bit_nb) >> 3] & \
    (((unsigned char) 1) << ((bit_nb) & 7)))

/* determines if bit number "bit_nb" is set in array "arr" */
#define IS_BIT_CLEARED(arr, bit_nb) (! IS_BIT_SET(arr, bit_nb))

/* resets bit "bit_nb" in array "arr" */
#define RESET_BIT(arr, bit_nb) (((unsigned char*) arr)[(bit_nb) >> 3] &= ~(((unsigned char) 1) << ((bit_nb) & 7)))

/* sets bit "bit_nb" in array "arr" */
#define SET_BIT(arr, bit_nb)   (((unsigned char*) arr)[(bit_nb) >> 3] |=   ((unsigned char) 1) << ((bit_nb) & 7))



/* set the first nb_bits in array arr */
inline void SET_ALL_BITS(unsigned char* arr, unsigned long nb_bits)
{
    memset(arr, 255, nb_bits >> 3);
    if(nb_bits & 7) {
        arr[nb_bits >> 3] |= ((unsigned char) 255) >> (8 - (nb_bits & 7));
    }
}

/* reset the first nb_bits in array arr */
inline void RESET_ALL_BITS(unsigned char* arr, unsigned long nb_bits)
{
    memset(arr, 0, nb_bits >> 3);
    if(nb_bits & 7) {
        arr[nb_bits >> 3] &= ((unsigned char) 255) << (nb_bits & 7);
    }
}

#endif