1/*****************************************************************************/
2/* */
3/* bitops.c */
4/* */
5/* Single bit operations */
6/* */
7/* */
8/* */
9/* (C) 1998 Ullrich von Bassewitz */
10/* Wacholderweg 14 */
11/* D-70597 Stuttgart */
12/* EMail: uz@musoftware.de */
13/* */
14/* */
15/* This software is provided 'as-is', without any expressed or implied */
16/* warranty. In no event will the authors be held liable for any damages */
17/* arising from the use of this software. */
18/* */
19/* Permission is granted to anyone to use this software for any purpose, */
20/* including commercial applications, and to alter it and redistribute it */
21/* freely, subject to the following restrictions: */
22/* */
23/* 1. The origin of this software must not be misrepresented; you must not */
24/* claim that you wrote the original software. If you use this software */
25/* in a product, an acknowledgment in the product documentation would be */
26/* appreciated but is not required. */
27/* 2. Altered source versions must be plainly marked as such, and must not */
28/* be misrepresented as being the original software. */
29/* 3. This notice may not be removed or altered from any source */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36#include "bitops.h"
37
38
39
40/*****************************************************************************/
41/* Code */
42/*****************************************************************************/
43
44
45
46unsigned BitFind (unsigned long Val)
47/* Find the first bit that is set in Val. Val must *not* be zero */
48{
49 unsigned long Mask;
50 unsigned Bit;
51
52 /* Search for the bits */
53 Mask = 1;
54 Bit = 0;
55 while (1) {
56 if (Val & Mask) {
57 return Bit;
58 }
59 Mask <<= 1;
60 ++Bit;
61 }
62}
63
64
65
66void BitSet (void* Data, unsigned Bit)
67/* Set a bit in a char array */
68{
69 /* Make a char pointer */
70 unsigned char* D = Data;
71
72 /* Set the bit */
73 D [Bit / 8] |= 0x01 << (Bit % 8);
74}
75
76
77
78void BitReset (void* Data, unsigned Bit)
79/* Reset a bit in a char array */
80{
81 /* Make a char pointer */
82 unsigned char* D = Data;
83
84 /* Set the bit */
85 D [Bit / 8] &= ~(0x01 << (Bit % 8));
86}
87
88
89
90int BitIsSet (void* Data, unsigned Bit)
91/* Check if a bit is set in a char array */
92{
93 /* Make a char pointer */
94 unsigned char* D = Data;
95
96 /* Check the bit state */
97 return (D [Bit / 8] & (0x01 << (Bit % 8))) != 0;
98}
99
100
101
102int BitIsReset (void* Data, unsigned Bit)
103/* Check if a bit is reset in a char array */
104{
105 /* Make a char pointer */
106 unsigned char* D = Data;
107
108 /* Check the bit state */
109 return (D [Bit / 8] & (0x01 << (Bit % 8))) == 0;
110}
111
112
113
114void BitMerge (void* Target, const void* Source, unsigned Size)
115/* Merge the bits of two char arrays (that is, do an or for the full array) */
116{
117 /* Make char arrays */
118 unsigned char* T = Target;
119 const unsigned char* S = Source;
120
121 /* Merge the arrays */
122 while (Size--) {
123 *T++ |= *S++;
124 }
125}
126