BDE 4.39.x 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.
177///
178/// See @ref bdlb_bitmaskutil
180
181 // PUBLIC TYPES
182 enum {
183 k_BITS_PER_UINT32 = 32, // number of bits in type 'uint32_t'
184
185 k_BITS_PER_UINT64 = 64 // number of bits in type 'uint64_t'
186 };
187
188 // CLASS METHODS
189 static bsl::uint32_t eq(int index);
190
191 /// Return the unsigned integral value having the bit at the specified
192 /// `index` position set to 1, and all other bits set to 0.
193 ///
194 /// \pre The behavior is undefined unless `0 <= index <= # of bits in result`.
195 static bsl::uint64_t eq64(int index);
196
197 static bsl::uint32_t ge(int index);
198
199 /// Return the unsigned integral value having all bits at positions
200 /// greater than or equal to the specified `index` set to 1, and all other bits set to 0.
201 ///
202 /// \pre The behavior is undefined unless
203 /// `0 <= index <= # of bits in result`.
204 static bsl::uint64_t ge64(int index);
205
206 static bsl::uint32_t gt(int index);
207
208 /// Return the unsigned integral value having all bits at positions
209 /// greater than the specified `index` set to 1, and all other bits set to 0.
210 ///
211 /// \pre The behavior is undefined unless
212 /// `0 <= index <= # of bits in result`.
213 static bsl::uint64_t gt64(int index);
214
215 static bsl::uint32_t le(int index);
216
217 /// Return the unsigned integral value having all bits at positions less
218 /// than or equal to the specified `index` set to 1, and all other bits set to 0.
219 ///
220 /// \pre The behavior is undefined unless
221 /// `0 <= index <= # of bits in result`.
222 static bsl::uint64_t le64(int index);
223
224 static bsl::uint32_t lt(int index);
225
226 /// Return the unsigned integral value having all bits at positions less
227 /// than the specified `index` set to 1, and all other bits set to 0.
228 ///
229 /// \pre The behavior is undefined unless
230 /// `0 <= index <= # of bits in result`.
231 static bsl::uint64_t lt64(int index);
232
233 static bsl::uint32_t ne(int index);
234
235 /// Return the unsigned integral value having the bit at the specified
236 /// `index` position set to 0, and all other bits set to 1.
237 ///
238 /// \pre The behavior is undefined unless `0 <= index <= # of bits in result`.
239 static bsl::uint64_t ne64(int index);
240
241 static bsl::uint32_t one(int index, int numBits);
242
243 /// Return the unsigned integral value having the specified `numBits`
244 /// starting at the specified `index` set to 1, and all other bits set to 0.
245 ///
246 /// \pre The behavior is undefined unless `0 <= index`,
247 /// `0 <= numBits`, and `index + numBits <= # of bits in result`.
248 static bsl::uint64_t one64(int index, int numBits);
249
250 static bsl::uint32_t zero(int index, int numBits);
251
252 /// Return the unsigned integral value having the specified `numBits`
253 /// starting at the specified `index` set to 0, and all other bits set to 1.
254 ///
255 /// \pre The behavior is undefined unless `0 <= index`,
256 /// `0 <= numBits`, and `index + numBits <= # of bits in result`.
257 static bsl::uint64_t zero64(int index, int numBits);
258};
259
260// ============================================================================
261// INLINE DEFINITIONS
262// ============================================================================
263
264 // ------------------
265 // struct BitMaskUtil
266 // ------------------
267
268// CLASS METHODS
269inline
270bsl::uint32_t BitMaskUtil::eq(int index)
271{
272 BSLS_ASSERT(0 <= index);
273 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
274
276 index < static_cast<int>(k_BITS_PER_UINT32))
277 ? 1u << index
278 : 0;
279}
280
281inline
282bsl::uint64_t BitMaskUtil::eq64(int index)
283{
284 BSLS_ASSERT(0 <= index);
285 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
286
288 index < static_cast<int>(k_BITS_PER_UINT64))
289 ? 1LL << index
290 : 0;
291}
292
293inline
294bsl::uint32_t BitMaskUtil::ge(int index)
295{
296 BSLS_ASSERT(0 <= index);
297 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
298
300 index < static_cast<int>(k_BITS_PER_UINT32))
301 ? ~0U << index
302 : 0;
303}
304
305inline
306bsl::uint64_t BitMaskUtil::ge64(int index)
307{
308 BSLS_ASSERT(0 <= index);
309 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
310
312 index < static_cast<int>(k_BITS_PER_UINT64))
313 ? ~0ULL << index
314 : 0;
315}
316
317inline
318bsl::uint32_t BitMaskUtil::gt(int index)
319{
320 BSLS_ASSERT(0 <= index);
321 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
322
323 ++index;
325 index < static_cast<int>(k_BITS_PER_UINT32))
326 ? ~0U << index
327 : 0;
328}
329
330inline
331bsl::uint64_t BitMaskUtil::gt64(int index)
332{
333 BSLS_ASSERT(0 <= index);
334 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
335
336 ++index;
338 index < static_cast<int>(k_BITS_PER_UINT64))
339 ? ~0ULL << index
340 : 0;
341}
342
343inline
344bsl::uint32_t BitMaskUtil::le(int index)
345{
346 BSLS_ASSERT(0 <= index);
347 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
348
349 ++index;
351 index < static_cast<int>(k_BITS_PER_UINT32))
352 ? ~(~0U << index)
353 : -1;
354}
355
356inline
357bsl::uint64_t BitMaskUtil::le64(int index)
358{
359 BSLS_ASSERT(0 <= index);
360 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
361
362 ++index;
364 index < static_cast<int>(k_BITS_PER_UINT64))
365 ? ~(~0ULL << index)
366 : -1LL;
367}
368
369inline
370bsl::uint32_t BitMaskUtil::lt(int index)
371{
372 BSLS_ASSERT(0 <= index);
373 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
374
376 index < static_cast<int>(k_BITS_PER_UINT32))
377 ? ~(~0U << index)
378 : -1;
379}
380
381inline
382bsl::uint64_t BitMaskUtil::lt64(int index)
383{
384 BSLS_ASSERT(0 <= index);
385 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
386
388 index < static_cast<int>(k_BITS_PER_UINT64))
389 ? ~(~0ULL << index)
390 : -1LL;
391}
392
393inline
394bsl::uint32_t BitMaskUtil::ne(int index)
395{
396 BSLS_ASSERT(0 <= index);
397 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT32));
398
400 index >= static_cast<int>(k_BITS_PER_UINT32))
401 ? -1
402 : ~(1u << index);
403}
404
405inline
406bsl::uint64_t BitMaskUtil::ne64(int index)
407{
408 BSLS_ASSERT(0 <= index);
409 BSLS_ASSERT( index <= static_cast<int>(k_BITS_PER_UINT64));
410
412 index >= static_cast<int>(k_BITS_PER_UINT64))
413 ? -1LL
414 : ~(1LL << index);
415}
416
417inline
418bsl::uint32_t BitMaskUtil::one(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_UINT32));
423
424 return lt(index + numBits) & ge(index);
425}
426
427inline
428bsl::uint64_t BitMaskUtil::one64(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_UINT64));
433
434 return lt64(index + numBits) & ge64(index);
435}
436
437inline
438bsl::uint32_t BitMaskUtil::zero(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_UINT32));
443
444 return lt(index) | ge(index + numBits);
445}
446
447inline
448bsl::uint64_t BitMaskUtil::zero64(int index, int numBits)
449{
450 BSLS_ASSERT(0 <= index);
451 BSLS_ASSERT(0 <= numBits);
452 BSLS_ASSERT(index + numBits <= static_cast<int>(k_BITS_PER_UINT64));
453
454 return lt64(index) | ge64(index + numBits);
455}
456
457} // close package namespace
458
459
460#endif
461
462// ----------------------------------------------------------------------------
463// Copyright 2015 Bloomberg Finance L.P.
464//
465// Licensed under the Apache License, Version 2.0 (the "License");
466// you may not use this file except in compliance with the License.
467// You may obtain a copy of the License at
468//
469// http://www.apache.org/licenses/LICENSE-2.0
470//
471// Unless required by applicable law or agreed to in writing, software
472// distributed under the License is distributed on an "AS IS" BASIS,
473// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
474// See the License for the specific language governing permissions and
475// limitations under the License.
476// ----------------------------- END-OF-FILE ----------------------------------
477
478/** @} */
479/** @} */
480/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#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:179
static bsl::uint64_t ne64(int index)
Definition bdlb_bitmaskutil.h:406
static bsl::uint64_t zero64(int index, int numBits)
Definition bdlb_bitmaskutil.h:448
static bsl::uint32_t ge(int index)
Definition bdlb_bitmaskutil.h:294
static bsl::uint64_t le64(int index)
Definition bdlb_bitmaskutil.h:357
static bsl::uint64_t gt64(int index)
Definition bdlb_bitmaskutil.h:331
static bsl::uint64_t eq64(int index)
Definition bdlb_bitmaskutil.h:282
static bsl::uint32_t ne(int index)
Definition bdlb_bitmaskutil.h:394
static bsl::uint32_t zero(int index, int numBits)
Definition bdlb_bitmaskutil.h:438
static bsl::uint32_t lt(int index)
Definition bdlb_bitmaskutil.h:370
static bsl::uint32_t le(int index)
Definition bdlb_bitmaskutil.h:344
static bsl::uint64_t one64(int index, int numBits)
Definition bdlb_bitmaskutil.h:428
static bsl::uint64_t ge64(int index)
Definition bdlb_bitmaskutil.h:306
static bsl::uint64_t lt64(int index)
Definition bdlb_bitmaskutil.h:382
static bsl::uint32_t eq(int index)
Definition bdlb_bitmaskutil.h:270
static bsl::uint32_t gt(int index)
Definition bdlb_bitmaskutil.h:318
static bsl::uint32_t one(int index, int numBits)
Definition bdlb_bitmaskutil.h:418
@ k_BITS_PER_UINT64
Definition bdlb_bitmaskutil.h:185
@ k_BITS_PER_UINT32
Definition bdlb_bitmaskutil.h:183