BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_bitmaskutil.h
Go to the documentation of this file.
1/// @file bdlb_bitmaskutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_bitmaskutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_BITMASKUTIL
9#define INCLUDED_BDLB_BITMASKUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_bitmaskutil bdlb_bitmaskutil
15/// @brief Provide simple mask values of `uint32_t` and `uint64_t` types.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_bitmaskutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_bitmaskutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_bitmaskutil-classes"> Classes </a>
26/// * <a href="#bdlb_bitmaskutil-description"> Description </a>
27/// * <a href="#bdlb_bitmaskutil-usage"> Usage </a>
28/// * <a href="#bdlb_bitmaskutil-example-1-creation-of-simple-bit-masks"> Example 1: Creation of Simple Bit Masks </a>
29///
30/// # Purpose {#bdlb_bitmaskutil-purpose}
31/// Provide simple mask values of `uint32_t` and `uint64_t` types.
32///
33/// # Classes {#bdlb_bitmaskutil-classes}
34///
35/// - bdlb::BitMaskUtil: namespace for bit-level mask operations
36///
37/// # Description {#bdlb_bitmaskutil-description}
38/// This component provides a utility `struct`,
39/// `bdlb::BitMaskUtil`, that serves as a namespace for a collection of
40/// functions that provide simple binary masks.
41///
42/// ## Usage {#bdlb_bitmaskutil-usage}
43///
44///
45/// This section illustrates intended use of this component.
46///
47/// ### Example 1: Creation of Simple Bit Masks {#bdlb_bitmaskutil-example-1-creation-of-simple-bit-masks}
48///
49///
50/// The following usage examples illustrate how some of the methods provided by
51/// this component are used. Note that, in all of these examples, the low-order
52/// bit is considered bit 0 and resides on the right edge of the bit string.
53///
54/// First, the `ge` function takes a single argument, `index`, and returns a bit
55/// mask with all bits below the specified `index` cleared and all bits at or
56/// above the `index` set:
57/// @code
58/// +-------------------------------------------------------------------------+
59/// | 'bdlb::BitMaskUtil::ge(16)' in binary: |
60/// | |
61/// | 'index': bit 16: * |
62/// | All bits at and above bit 16 are set: 11111111111111110000000000000000 |
63/// +-------------------------------------------------------------------------+
64///
65/// const uint32_t expGe = 0xffff0000;
66/// assert(expGe == bdlb::BitMaskUtil::ge(16));
67/// @endcode
68/// Next, the `lt` function returns a bit mask with all bits at or above the
69/// specified `index` cleared, and all bits below `index` set. `lt` and `ge`
70/// return the complement of each other if passed the same `index`:
71/// @code
72/// +-------------------------------------------------------------------------+
73/// | 'bdlb::BitMaskUtil::lt(16)' in binary: |
74/// | |
75/// | 'index': bit 16: * |
76/// | All bits below bit 16 are set: 00000000000000001111111111111111 |
77/// +-------------------------------------------------------------------------+
78///
79/// const uint32_t expLt = 0x0000ffff;
80/// assert(expLt == bdlb::BitMaskUtil::lt(16));
81///
82/// assert(expGe == ~expLt);
83/// @endcode
84/// Then, the `eq` function returns a bit mask with only the bit at the
85/// specified `index` set:
86/// @code
87/// +-------------------------------------------------------------------------+
88/// | 'bdlb::BitMaskUtil::eq(23)' in binary: |
89/// | |
90/// | 'index': bit 23: * |
91/// | Only bit 23 is set: 00000000100000000000000000000000 |
92/// +-------------------------------------------------------------------------+
93///
94/// const uint32_t expEq = 0x00800000;
95/// assert(expEq == bdlb::BitMaskUtil::eq(23));
96/// @endcode
97/// Now, the `ne` function returns a bit mask with only the bit at the specified
98/// `index` cleared. `ne` and `eq` return the complement of each other for a
99/// given `index`:
100/// @code
101/// +-------------------------------------------------------------------------+
102/// | 'bdlb::BitMaskUtil::ne(23)' in binary: |
103/// | |
104/// | 'index': bit 23: * |
105/// | All bits other than bit 16 are set: 11111111011111111111111111111111 |
106/// +-------------------------------------------------------------------------+
107///
108/// const uint32_t expNe = 0xff7fffff;
109/// assert(expNe == bdlb::BitMaskUtil::ne(23));
110///
111/// assert(expEq == ~expNe);
112/// @endcode
113/// Finally, `one` and `zero` return a bit mask with all bits within a specified
114/// range starting from a specified `index` either set or cleared, respectively.
115/// For the same arguments, `one` and `zero` return the complement of each
116/// other:
117/// @code
118/// +-------------------------------------------------------------------------+
119/// | 'bdlb::BitMaskUtil::one(16, 4)' in binary: |
120/// | |
121/// | bit 16: * |
122/// | 4 bits starting at bit 16: **** |
123/// | Result: only those bits set: 00000000000011110000000000000000 |
124/// +-------------------------------------------------------------------------+
125///
126/// const uint32_t expOne = 0x000f0000;
127/// assert(expOne == bdlb::BitMaskUtil::one(16, 4));
128///
129/// +-------------------------------------------------------------------------+
130/// | 'bdlb::BitMaskUtil::zero(16, 4)' in binary: |
131/// | |
132/// | bit 16: * |
133/// | 4 bits starting at bit 16: **** |
134/// | Result: only those bits cleared: 11111111111100001111111111111111 |
135/// +-------------------------------------------------------------------------+
136///
137/// const uint32_t expZero = 0xfff0ffff;
138/// assert(expZero == bdlb::BitMaskUtil::zero(16, 4));
139///
140/// assert(expZero == ~expOne);
141/// @endcode
142/// @}
143/** @} */
144/** @} */
145
146/** @addtogroup bdl
147 * @{
148 */
149/** @addtogroup bdlb
150 * @{
151 */
152/** @addtogroup bdlb_bitmaskutil
153 * @{
154 */
155
156#include <bdlscm_version.h>
157
158#include <bdlb_bitutil.h>
159
160#include <bsls_assert.h>
161#include <bsls_performancehint.h>
162#include <bsls_review.h>
163
164#include <bsl_cstdint.h>
165
166
167namespace bdlb {
168
169 // ==================
170 // struct BitMaskUtil
171 // ==================
172
173/// This utility `struct` provides a namespace for a set of bit-level,
174/// stateless functions that take one or two `int` arguments and return
175/// masks of the built-in 32- and 64-bit integer types `uint32_t` and
176/// `uint64_t`, respectively.
178
179 // PUBLIC TYPES
180 enum {
181 k_BITS_PER_UINT32 = 32, // number of bits in type 'uint32_t'
182
183 k_BITS_PER_UINT64 = 64 // number of bits in type 'uint64_t'
184 };
185
186 // CLASS METHODS
187 static bsl::uint32_t eq(int index);
188
189 /// Return the unsigned integral value having the bit at the specified
190 /// `index` position set to 1, and all other bits set to 0. The
191 /// behavior is undefined unless `0 <= index <= # of bits in result`.
192 static bsl::uint64_t eq64(int index);
193
194 static bsl::uint32_t ge(int index);
195
196 /// Return the unsigned integral value having all bits at positions
197 /// greater than or equal to the specified `index` set to 1, and all
198 /// other bits set to 0. The behavior is undefined unless
199 /// `0 <= index <= # of bits in result`.
200 static bsl::uint64_t ge64(int index);
201
202 static bsl::uint32_t gt(int index);
203
204 /// Return the unsigned integral value having all bits at positions
205 /// greater than the specified `index` set to 1, and all other bits set
206 /// to 0. The behavior is undefined unless
207 /// `0 <= index <= # of bits in result`.
208 static bsl::uint64_t gt64(int index);
209
210 static bsl::uint32_t le(int index);
211
212 /// Return the unsigned integral value having all bits at positions less
213 /// than or equal to the specified `index` set to 1, and all other bits
214 /// set to 0. The behavior is undefined unless
215 /// `0 <= index <= # of bits in result`.
216 static bsl::uint64_t le64(int index);
217
218 static bsl::uint32_t lt(int index);
219
220 /// Return the unsigned integral value having all bits at positions less
221 /// than the specified `index` set to 1, and all other bits set to 0.
222 /// The behavior is undefined unless
223 /// `0 <= index <= # of bits in result`.
224 static bsl::uint64_t lt64(int index);
225
226 static bsl::uint32_t ne(int index);
227
228 /// Return the unsigned integral value having the bit at the specified
229 /// `index` position set to 0, and all other bits set to 1. The
230 /// behavior is undefined unless `0 <= index <= # of bits in result`.
231 static bsl::uint64_t ne64(int index);
232
233 static bsl::uint32_t one(int index, int numBits);
234
235 /// Return the unsigned integral value having the specified `numBits`
236 /// starting at the specified `index` set to 1, and all other bits set
237 /// to 0. The behavior is undefined unless `0 <= index`,
238 /// `0 <= numBits`, and `index + numBits <= # of bits in result`.
239 static bsl::uint64_t one64(int index, int numBits);
240
241 static bsl::uint32_t zero(int index, int numBits);
242
243 /// Return the unsigned integral value having the specified `numBits`
244 /// starting at the specified `index` set to 0, and all other bits set
245 /// to 1. The behavior is undefined unless `0 <= index`,
246 /// `0 <= numBits`, and `index + numBits <= # of bits in result`.
247 static bsl::uint64_t zero64(int index, int numBits);
248};
249
250// ============================================================================
251// INLINE DEFINITIONS
252// ============================================================================
253
254 // ------------------
255 // struct BitMaskUtil
256 // ------------------
257
258// CLASS METHODS
259inline
260bsl::uint32_t BitMaskUtil::eq(int index)
261{
262 BSLS_ASSERT(0 <= index);
263 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
264
266 index < static_cast<int>(k_BITS_PER_UINT32))
267 ? 1u << index
268 : 0;
269}
270
271inline
272bsl::uint64_t BitMaskUtil::eq64(int index)
273{
274 BSLS_ASSERT(0 <= index);
275 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
276
278 index < static_cast<int>(k_BITS_PER_UINT64))
279 ? 1LL << index
280 : 0;
281}
282
283inline
284bsl::uint32_t BitMaskUtil::ge(int index)
285{
286 BSLS_ASSERT(0 <= index);
287 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
288
290 index < static_cast<int>(k_BITS_PER_UINT32))
291 ? ~0U << index
292 : 0;
293}
294
295inline
296bsl::uint64_t BitMaskUtil::ge64(int index)
297{
298 BSLS_ASSERT(0 <= index);
299 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
300
302 index < static_cast<int>(k_BITS_PER_UINT64))
303 ? ~0ULL << index
304 : 0;
305}
306
307inline
308bsl::uint32_t BitMaskUtil::gt(int index)
309{
310 BSLS_ASSERT(0 <= index);
311 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
312
313 ++index;
315 index < static_cast<int>(k_BITS_PER_UINT32))
316 ? ~0U << index
317 : 0;
318}
319
320inline
321bsl::uint64_t BitMaskUtil::gt64(int index)
322{
323 BSLS_ASSERT(0 <= index);
324 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
325
326 ++index;
328 index < static_cast<int>(k_BITS_PER_UINT64))
329 ? ~0ULL << index
330 : 0;
331}
332
333inline
334bsl::uint32_t BitMaskUtil::le(int index)
335{
336 BSLS_ASSERT(0 <= index);
337 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
338
339 ++index;
341 index < static_cast<int>(k_BITS_PER_UINT32))
342 ? ~(~0U << index)
343 : -1;
344}
345
346inline
347bsl::uint64_t BitMaskUtil::le64(int index)
348{
349 BSLS_ASSERT(0 <= index);
350 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
351
352 ++index;
354 index < static_cast<int>(k_BITS_PER_UINT64))
355 ? ~(~0ULL << index)
356 : -1LL;
357}
358
359inline
360bsl::uint32_t BitMaskUtil::lt(int index)
361{
362 BSLS_ASSERT(0 <= index);
363 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
364
366 index < static_cast<int>(k_BITS_PER_UINT32))
367 ? ~(~0U << index)
368 : -1;
369}
370
371inline
372bsl::uint64_t BitMaskUtil::lt64(int index)
373{
374 BSLS_ASSERT(0 <= index);
375 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
376
378 index < static_cast<int>(k_BITS_PER_UINT64))
379 ? ~(~0ULL << index)
380 : -1LL;
381}
382
383inline
384bsl::uint32_t BitMaskUtil::ne(int index)
385{
386 BSLS_ASSERT(0 <= index);
387 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
388
390 index >= static_cast<int>(k_BITS_PER_UINT32))
391 ? -1
392 : ~(1u << index);
393}
394
395inline
396bsl::uint64_t BitMaskUtil::ne64(int index)
397{
398 BSLS_ASSERT(0 <= index);
399 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
400
402 index >= static_cast<int>(k_BITS_PER_UINT64))
403 ? -1LL
404 : ~(1LL << index);
405}
406
407inline
408bsl::uint32_t BitMaskUtil::one(int index, int numBits)
409{
410 BSLS_ASSERT(0 <= index);
411 BSLS_ASSERT(0 <= numBits);
412 BSLS_ASSERT(index + numBits <= static_cast<int>(k_BITS_PER_UINT32));
413
414 return lt(index + numBits) & ge(index);
415}
416
417inline
418bsl::uint64_t BitMaskUtil::one64(int index, int numBits)
419{
420 BSLS_ASSERT(0 <= index);
421 BSLS_ASSERT(0 <= numBits);
422 BSLS_ASSERT(index + numBits <= static_cast<int>(k_BITS_PER_UINT64));
423
424 return lt64(index + numBits) & ge64(index);
425}
426
427inline
428bsl::uint32_t BitMaskUtil::zero(int index, int numBits)
429{
430 BSLS_ASSERT(0 <= index);
431 BSLS_ASSERT(0 <= numBits);
432 BSLS_ASSERT(index + numBits <= static_cast<int>(k_BITS_PER_UINT32));
433
434 return lt(index) | ge(index + numBits);
435}
436
437inline
438bsl::uint64_t BitMaskUtil::zero64(int index, int numBits)
439{
440 BSLS_ASSERT(0 <= index);
441 BSLS_ASSERT(0 <= numBits);
442 BSLS_ASSERT(index + numBits <= static_cast<int>(k_BITS_PER_UINT64));
443
444 return lt64(index) | ge64(index + numBits);
445}
446
447} // close package namespace
448
449
450#endif
451
452// ----------------------------------------------------------------------------
453// Copyright 2015 Bloomberg Finance L.P.
454//
455// Licensed under the Apache License, Version 2.0 (the "License");
456// you may not use this file except in compliance with the License.
457// You may obtain a copy of the License at
458//
459// http://www.apache.org/licenses/LICENSE-2.0
460//
461// Unless required by applicable law or agreed to in writing, software
462// distributed under the License is distributed on an "AS IS" BASIS,
463// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
464// See the License for the specific language governing permissions and
465// limitations under the License.
466// ----------------------------- END-OF-FILE ----------------------------------
467
468/** @} */
469/** @} */
470/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_PERFORMANCEHINT_PREDICT_LIKELY(expr)
Definition bsls_performancehint.h:451
#define BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(expr)
Definition bsls_performancehint.h:452
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_bitmaskutil.h:177
static bsl::uint64_t ne64(int index)
Definition bdlb_bitmaskutil.h:396
static bsl::uint64_t zero64(int index, int numBits)
Definition bdlb_bitmaskutil.h:438
static bsl::uint32_t ge(int index)
Definition bdlb_bitmaskutil.h:284
static bsl::uint64_t le64(int index)
Definition bdlb_bitmaskutil.h:347
static bsl::uint64_t gt64(int index)
Definition bdlb_bitmaskutil.h:321
static bsl::uint64_t eq64(int index)
Definition bdlb_bitmaskutil.h:272
static bsl::uint32_t ne(int index)
Definition bdlb_bitmaskutil.h:384
static bsl::uint32_t zero(int index, int numBits)
Definition bdlb_bitmaskutil.h:428
static bsl::uint32_t lt(int index)
Definition bdlb_bitmaskutil.h:360
static bsl::uint32_t le(int index)
Definition bdlb_bitmaskutil.h:334
static bsl::uint64_t one64(int index, int numBits)
Definition bdlb_bitmaskutil.h:418
static bsl::uint64_t ge64(int index)
Definition bdlb_bitmaskutil.h:296
static bsl::uint64_t lt64(int index)
Definition bdlb_bitmaskutil.h:372
static bsl::uint32_t eq(int index)
Definition bdlb_bitmaskutil.h:260
static bsl::uint32_t gt(int index)
Definition bdlb_bitmaskutil.h:308
static bsl::uint32_t one(int index, int numBits)
Definition bdlb_bitmaskutil.h:408
@ k_BITS_PER_UINT64
Definition bdlb_bitmaskutil.h:183
@ k_BITS_PER_UINT32
Definition bdlb_bitmaskutil.h:181