2 .TH bits 3mLib "20 June 1999" mLib
4 bits \- portable bit manipulation macros
7 .B "#include <mLib/bits.h>"
10 .BI "uint16 U16(" v );
11 .BI "uint32 U32(" v );
13 .BI "octet LSL8(" v ", " s );
14 .BI "octet LSR8(" v ", " s );
15 .BI "uint16 LSL16(" v ", " s );
16 .BI "uint16 LSR16(" v ", " s );
17 .BI "uint32 LSL32(" v ", " s );
18 .BI "uint32 LSR32(" v ", " s );
20 .BI "octet ROL8(" v ", " s );
21 .BI "octet ROR8(" v ", " s );
22 .BI "uint16 ROL16(" v ", " s );
23 .BI "uint16 ROR16(" v ", " s );
24 .BI "uint32 ROL32(" v ", " s );
25 .BI "uint32 ROR32(" v ", " s );
27 .BI "octet GETBYTE(" p ", " o );
28 .BI "void PUTBYTE(" p ", " o ", " v );
30 .BI "octet LOAD8(" p );
31 .BI "void STORE8(" p ", " v );
33 .BI "uint16 LOAD16_B(" p );
34 .BI "uint16 LOAD16_L(" p );
35 .BI "uint16 LOAD16(" p );
36 .BI "void STORE16_B(" p ", " v );
37 .BI "void STORE16_L(" p ", " v );
38 .BI "void STORE16(" p ", " v );
40 .BI "uint32 LOAD32_B(" p );
41 .BI "uint32 LOAD32_L(" p );
42 .BI "uint32 LOAD32(" p );
43 .BI "void STORE32_B(" p ", " v );
44 .BI "void STORE32_L(" p ", " v );
45 .BI "void STORE32(" p ", " v );
50 contains a number of useful definitions for portably dealing with bit-
51 and byte-level manipulation of larger quantities. It declares three
57 This is intended to be used when a character array is used to represent
58 the octets of some external data format. Note that on some
61 type may occupy more than 8 bits.
65 .BR "unsigned short" .
66 Intended to be used when a 16-bit value is required. This type is
67 always capable of representing any 16-bit unsigned value, but the actual
68 type may be wider than 16 bits and will require masking.
71 Equivalent to some (architecture-dependent) standard type. Capable of
72 representing any unsigned 32-bit value, although the the actual type may
73 be wider than 32 bits.
80 contain bitmasks appropriate for discarding additional bits from a value
81 before use as an 8-, 16- or 32-bit quantity. The macros
86 perform masking and type-coercion on a value, and may be more useful in
89 yields a value of type
91 which contains (only) the least-significant 16 bits of
98 perform left and right logical shift operations on values of width
107 The first argument, written
109 is the value to shift, and the second, written
111 is the number of bits to shift. The value
115 before use. Similarly, the macros
119 perform left and right rotations (cyclic shifts) on values of width
121 These macros are all written in such a way as to maximize portability.
122 A compiler may be able to spot that simple machine instructions will
123 suffice in many cases, although that's not the main objective.
136 perform transformations between
138 quantities and arrays of octets. For example,
140 returns the 32-bit value stored in the four octets starting at address
144 stores the 16-bit value
146 in the 2 octets starting at address
148 Values are loaded and stored such that the most significant octet is
149 assigned the lowest address (i.e., they use network, or big-endian byte
154 are also provided for
160 they use little-endian byte order. There are
161 explicit big-endian macros
165 too. The pointer arguments don't have to be pointers to octets; the
166 value arguments don't have to be of the right type. The macros perform
167 all appropriate type casting and masking. Again, these macros are
168 written with portability foremost in mind, although it seems they don't
169 actually perform at all badly in real use.
171 Mark Wooding, <mdw@nsict.org>