Library of Assembled Shared Sources
bit_manip.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @defgroup BitManip BitManip
44 * @brief A set of simple bit manipulation functions.
45 * @author Bram de Greve
46 * @date 2003
47 *
48 * This module groups simple bit manipulation functions you can use on bitfields. These bitfields
49 * can be of any type for which the bit operators like &=, |=, ... are defined. Typically, you'll
50 * use @c int, @c char, @c unsigned, ...
51 *
52 * The functions are split in two groups. The @e bit routines (with the word @c Bit in their
53 * names :) and the @e mask routines (with the word @c Mask). The groups are very alike, except in
54 * the way you address the bits to affect. With the @e bit group you will address one single bit
55 * at one time, and you will indicate it by its index in the bit field (i.e. bit 0 is 0x01, bit 1
56 * is 0x02, bit 2 is 0x04, ...). With the @e mask group, you can address multiple bits at once by
57 * setting these bits in the mask (e.g. to address bits 0 and 2, you'll use 0x05 as mask). These
58 * mask must be of the same type of the bitfield.
59 *
60 * With the routines you can set bits to 1; clear bits to 0; flip bits (0 becomes 1, 1 becomes 0);
61 * set, clear or flip bits if a condition is fullfilled; set a bit to a given value (0 or 1);
62 * check if a bit is set. For all these operations you'll find a function in both the @c bit group
63 * as in the @c mask group, except for the last operation: to check if a bit is set. In the @c bit
64 * group you'll find one function checkBit which will return true if the addressed bit is set.
65 * In the @c mask group you'll find two: checkMaskedAll and checkMaskedSome. the former only
66 * returns true if @e all bits addressed by the mask are set (an @e and operation), the latter
67 * already returns true if at least @e some of the addressed bits are set (an @e or operation).
68 */
69
70
71#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_BIT_MANIP_H
72#define LASS_GUARDIAN_OF_INCLUSION_UTIL_BIT_MANIP_H
73
74
75#include "util_common.h"
76#include "../num/basic_types.h"
77
78namespace lass
79{
80
81namespace util
82{
83
84template<typename T> inline void setBit(T& a_bits, size_t a_bit);
85template<typename T> inline void clearBit(T& a_bits, size_t a_bit);
86template<typename T> inline void flipBit(T& a_bits, size_t a_bit);
87template<typename T> inline void setBitIf(T& a_bits, size_t a_bit, bool a_condition);
88template<typename T> inline void clearBitIf(T& a_bits, size_t a_bit, bool a_condition);
89template<typename T> inline void flipBitIf(T& a_bits, size_t a_bit, bool a_condition);
90template<typename T> inline void setBitTo(T& a_bits, size_t a_bit, bool a_state);
91template<typename T> inline bool checkBit(T a_bits, size_t a_bit);
92
93template<typename T> inline void setMasked(T& a_bits, const T& a_mask);
94template<typename T> inline void clearMasked(T& a_bits, const T& a_mask);
95template<typename T> inline void flipMasked(T& a_bits, const T& a_mask);
96template<typename T> inline void setMaskedIf(T& a_bits, const T& a_mask, bool a_condition);
97template<typename T> inline void clearMaskedIf(T& a_bits, const T& a_mask, bool a_condition);
98template<typename T> inline void flipMaskedIf(T& a_bits, const T& a_mask, bool a_condition);
99template<typename T> inline void setMaskedTo(T& a_bits, const T& a_mask, bool a_state);
100template<typename T> inline bool checkMaskedAll(T a_bits, const T& a_mask);
101template<typename T> inline bool checkMaskedSome(T a_bits, const T& a_mask);
102
103template<typename T> inline size_t countBits(T bits);
104
105}
106
107}
108
109#include "bit_manip.inl"
110
111#endif
bool checkBit(T a_bits, size_t a_bit)
return true if a bit is set high.
bool checkMaskedSome(T a_bits, const T &a_mask)
Check the masked bits and return true if at least one is set.
void setBit(T &a_bits, size_t a_bit)
Set a bit high.
Definition bit_manip.inl:66
void setMaskedIf(T &a_bits, const T &a_mask, bool a_condition)
Set masked bits high if (and only if) a condition is fullfilled.
void setBitTo(T &a_bits, size_t a_bit, bool a_state)
set a bit to a given state.
void flipMaskedIf(T &a_bits, const T &a_mask, bool a_condition)
Flip the masked bits if (and only if) a condition is fullfilled.
bool checkMaskedAll(T a_bits, const T &a_mask)
Check the masked bits and return true if they are ALL set.
void flipBitIf(T &a_bits, size_t a_bit, bool a_condition)
flip a bit if (and only if) a condition is fullfilled (low->high, high->low).
void clearMasked(T &a_bits, const T &a_mask)
Set masked bits low.
void setMasked(T &a_bits, const T &a_mask)
Set masked bits high.
size_t countBits(T bits)
returns number of set bits in bits
void clearMaskedIf(T &a_bits, const T &a_mask, bool a_condition)
Set masked bits low if (and only if) a condition is fullfilled.
void clearBitIf(T &a_bits, size_t a_bit, bool a_condition)
set a bit low if (and only if) a condition is fullfilled is true.
void setBitIf(T &a_bits, size_t a_bit, bool a_condition)
set a bit high if (and only if) a condition is fullfilled.
void flipMasked(T &a_bits, const T &a_mask)
flip masked bits.
void clearBit(T &a_bits, size_t a_bit)
set a bit low.
Definition bit_manip.inl:80
void flipBit(T &a_bits, size_t a_bit)
flip state of a bit (low->high, high->low).
Definition bit_manip.inl:94
void setMaskedTo(T &a_bits, const T &a_mask, bool a_state)
Set the masked bits to a given state if (and only if) a condition is fullfilled.
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53