File: bitarray.h

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (49 lines) | stat: -rw-r--r-- 1,950 bytes parent folder | download | duplicates (4)
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
42
43
44
45
46
47
48
49
/*
 * Created by Ian "Goober5000" Warfield for the FreeSpace2 Source Code Project.
 * You may not sell or otherwise commercially exploit the source or things you
 * create based on the source.
 */ 



#ifndef _BIT_ARRAY_H
#define _BIT_ARRAY_H

#include "globalincs/pstypes.h"


// the following four functions are adapted from http://www.codeproject.com/cpp/BitArray.asp; their explanation
// is as follows:
/*
 * The Bit Array structure provides a compacted arrays of Booleans, with one bit for each Boolean value.
 * A 0 (1) bit corresponds to the Boolean value false (true), respectively. We can look at a stream of bytes
 * as a stream of bits; each byte contains 8 bits, so any n bytes hold n*8 bits. And the operation to
 * manipulate this stream or bit array is so easy, just read or change the bit's state or make any Boolean
 * operation on the whole bits array, like AND, OR, or XOR.
 *
 * As each byte contains 8 bits, we need to divide the bit number by 8 to reach the byte that holds the bit.
 * Then, we can seek to the right bit in the reached byte by the remainder of dividing the bit number by 8.
 * So to read or change the bit state, the operations will be like that.
 *
 * Note that to divide by 8, we need only to shift right by 3 (>>3), and to get the remainder of dividing
 * by 8, we need only to AND with 7 (&7).
 */

// returns bit state (0 or 1)
#define get_bit(array, bitnum)			((((ubyte *) array)[(bitnum) >> 3] >> ((bitnum) & 7)) & 1)

// sets bit to 1
#define set_bit(array, bitnum)			(((ubyte *) array)[(bitnum) >> 3] |= (1 << ((bitnum) & 7)))

// clears bit to 0
#define clear_bit(array, bitnum)		(((ubyte *) array)[(bitnum) >> 3] &= ~(1 << ((bitnum) & 7)))

// toggles bit (xor)
#define toggle_bit(array, bitnum)		(((ubyte *) array)[(bitnum) >> 3] ^= (1 << ((bitnum) & 7)))


// calculate number of bytes from number of bits
#define calculate_num_bytes(num_bits)	((num_bits >> 3) + 1)


#endif	// _BIT_ARRAY_H