2 .TH bits 3 "20 June 1999" mLib
4 bits \- portable bit manipulation macros
45 .B "#include <mLib/bits.h>"
48 .BI "uint16 U16(" v );
49 .BI "uint32 U32(" v );
51 .BI "octet LSL8(" v ", " s );
52 .BI "octet LSR8(" v ", " s );
53 .BI "uint16 LSL16(" v ", " s );
54 .BI "uint16 LSR16(" v ", " s );
55 .BI "uint32 LSL32(" v ", " s );
56 .BI "uint32 LSR32(" v ", " s );
58 .BI "octet ROL8(" v ", " s );
59 .BI "octet ROR8(" v ", " s );
60 .BI "uint16 ROL16(" v ", " s );
61 .BI "uint16 ROR16(" v ", " s );
62 .BI "uint32 ROL32(" v ", " s );
63 .BI "uint32 ROR32(" v ", " s );
65 .BI "octet GETBYTE(" p ", " o );
66 .BI "void PUTBYTE(" p ", " o ", " v );
68 .BI "octet LOAD8(" p );
69 .BI "void STORE8(" p ", " v );
71 .BI "uint16 LOAD16_B(" p );
72 .BI "uint16 LOAD16_L(" p );
73 .BI "uint16 LOAD16(" p );
74 .BI "void STORE16_B(" p ", " v );
75 .BI "void STORE16_L(" p ", " v );
76 .BI "void STORE16(" p ", " v );
78 .BI "uint32 LOAD32_B(" p );
79 .BI "uint32 LOAD32_L(" p );
80 .BI "uint32 LOAD32(" p );
81 .BI "void STORE32_B(" p ", " v );
82 .BI "void STORE32_L(" p ", " v );
83 .BI "void STORE32(" p ", " v );
88 contains a number of useful definitions for portably dealing with bit-
89 and byte-level manipulation of larger quantities. It declares three
95 This is intended to be used when a character array is used to represent
96 the octets of some external data format. Note that on some
99 type may occupy more than 8 bits.
103 .BR "unsigned short" .
104 Intended to be used when a 16-bit value is required. This type is
105 always capable of representing any 16-bit unsigned value, but the actual
106 type may be wider than 16 bits and will require masking.
109 Equivalent to some (architecture-dependent) standard type. Capable of
110 representing any unsigned 32-bit value, although the the actual type may
111 be wider than 32 bits.
118 contain bitmasks appropriate for discarding additional bits from a value
119 before use as an 8-, 16- or 32-bit quantity. The macros
124 perform masking and type-coercion on a value, and may be more useful in
125 general. For example,
127 yields a value of type
129 which contains (only) the least-significant 16 bits of
136 perform left and right logical shift operations on values of width
145 The first argument, written
147 is the value to shift, and the second, written
149 is the number of bits to shift. The value
153 before use. Similarly, the macros
157 perform left and right rotations (cyclic shifts) on values of width
159 These macros are all written in such a way as to maximize portability.
160 A compiler may be able to spot that simple machine instructions will
161 suffice in many cases, although that's not the main objective.
174 perform transformations between
176 quantities and arrays of octets. For example,
178 returns the 32-bit value stored in the four octets starting at address
182 stores the 16-bit value
184 in the 2 octets starting at address
186 Values are loaded and stored such that the most significant octet is
187 assigned the lowest address (i.e., they use network, or big-endian byte
192 are also provided for
198 they use little-endian byte order. There are
199 explicit big-endian macros
203 too. The pointer arguments don't have to be pointers to octets; the
204 value arguments don't have to be of the right type. The macros perform
205 all appropriate type casting and masking. Again, these macros are
206 written with portability foremost in mind, although it seems they don't
207 actually perform at all badly in real use.
211 Mark Wooding, <mdw@nsict.org>