ROSE 0.11.145.147
|
Bit operations on unsigned integers.
This namespace provides functions that operate at the bit level on unsigned integer types and avoid C/C++ undefined behavior.
Functions | |
template<typename Unsigned > | |
size_t | nBits (Unsigned=Unsigned(0)) |
Number of bits in a type or value. | |
template<typename Unsigned > | |
Unsigned | all (bool b=true) |
Generate a value with all bits set or cleared. | |
template<typename Unsigned > | |
Unsigned | lowMask (size_t n) |
Generate a value with low order bits set. | |
template<typename Unsigned > | |
Unsigned | allLsb (Unsigned src, size_t w, bool b=true) |
Set or clear the low-order w bits. | |
template<typename Unsigned > | |
Unsigned | highMask (size_t n) |
Generate a value with high order bits set. | |
template<typename Unsigned > | |
Unsigned | select (Unsigned cond, Unsigned a, Unsigned b) |
Combine two values based on a bit mask. | |
template<typename Unsigned > | |
Unsigned | shiftLeft (Unsigned src, size_t n, bool b=false) |
Left shift a value. | |
template<typename Unsigned > | |
Unsigned | shiftLeftLsb (Unsigned src, size_t w, size_t n, bool b=false) |
Left shift part of a value without affecting the rest. | |
template<typename Unsigned > | |
Unsigned | shiftRight (Unsigned src, size_t n, bool b=false) |
Right shift a value. | |
template<typename Unsigned > | |
Unsigned | shiftRightLsb (Unsigned src, size_t w, size_t n, bool b=false) |
Right shift part of a value without affecting the rest. | |
template<typename Unsigned > | |
Unsigned | position (size_t i) |
Generate a single-bit mask. | |
template<typename Unsigned > | |
Unsigned | positionLsb (Unsigned src, size_t w, size_t i) |
Generate a single-bit mask without affecting the high-order bits. | |
template<typename Unsigned > | |
Unsigned | mask (size_t least, size_t greatest) |
Generate a mask. | |
template<typename Unsigned > | |
Unsigned | maskLsb (Unsigned src, size_t w, size_t least, size_t greatest) |
Generate a mask without affecting other bits. | |
template<typename Unsigned > | |
bool | bit (Unsigned src, size_t i) |
Extract a single bit. | |
template<typename Unsigned > | |
bool | bitLsb (Unsigned src, size_t w, size_t i) |
Extract a single bit. | |
template<typename Unsigned > | |
bool | msb (Unsigned src) |
Most significant bit. | |
template<typename Unsigned > | |
bool | msbLsb (Unsigned src, size_t w) |
Most significant bit within lsb region. | |
template<typename Unsigned > | |
Unsigned | shiftRightSigned (Unsigned src, size_t n) |
Right shift replicating MSB. | |
template<typename Unsigned > | |
Unsigned | shiftRightSignedLsb (Unsigned src, size_t w, size_t n) |
Right shift low bits without affecting other bits. | |
template<typename Unsigned > | |
Unsigned | bits (Unsigned src, size_t least, size_t greatest) |
Extract part of a value. | |
template<typename Unsigned > | |
Unsigned | bitsLsb (Unsigned src, size_t w, size_t least, size_t greatest) |
Extract part of a value limited by width. | |
template<typename UnsignedTarget , typename UnsignedSource > | |
UnsignedTarget | convert (UnsignedSource x, bool b=false) |
Extend or truncate a value. | |
template<typename UnsignedTarget , typename UnsignedSource > | |
UnsignedTarget | convertSigned (UnsignedSource x) |
Sign extend or truncate a value. | |
template<typename Unsigned > | |
Unsigned | signExtend (Unsigned src, size_t n) |
Sign extend part of a value to the full width of the src type. | |
template<typename Unsigned > | |
Unsigned | signExtendLsb (Unsigned src, size_t n, size_t m) |
Sign extend part of value without affecting other bits. | |
template<typename Unsigned > | |
Unsigned | rotateLeft (Unsigned src, size_t n) |
Rotate bits left. | |
template<typename Unsigned > | |
Unsigned | rotateLeftLsb (Unsigned src, size_t w, size_t n) |
Rotate low-order bits left without affecting others. | |
template<typename Unsigned > | |
Unsigned | rotateRight (Unsigned src, size_t n) |
Rotate bits right. | |
template<typename Unsigned > | |
Unsigned | rotateRightLsb (Unsigned src, size_t w, size_t n) |
Rotate low-order bits right without affecting others. | |
template<typename Unsigned > | |
Unsigned | replicate (Unsigned src, size_t n) |
Replicate low-order bits to fill return value. | |
template<typename Unsigned > | |
Unsigned | replicateLsb (Unsigned src, size_t w, size_t n) |
Replicate low-order bits to fill region without affecting other bits. | |
template<typename Unsigned > | |
Sawyer::Optional< size_t > | highestSetBit (Unsigned src) |
Index of the highest set bit. | |
template<typename Unsigned > | |
size_t | nSet (Unsigned src) |
template<class T > | |
std::enable_if< std::is_integral< T >::value, T >::type | reverseBytes (const T &x) |
Reverse the bytes. | |
bool | isBigEndian () |
True if host is big endian. | |
bool | isLittleEndian () |
True if host is little endian. | |
template<class T > | |
std::enable_if< std::is_integral< T >::value, T >::type | toBigEndian (const T x) |
Convert integral value from host order to big endian. | |
template<class T > | |
std::enable_if< std::is_integral< T >::value, T >::type | toLittleEndian (const T x) |
Convert integral value from host order to little endian. | |
template<class T > | |
std::enable_if< std::is_integral< T >::value, T >::type | fromBigEndian (const T x) |
Convert integral value from big endian to host order. | |
template<class T > | |
std::enable_if< std::is_integral< T >::value, T >::type | fromLittleEndian (const T x) |
Convert integral value from little endian to host order. | |
|
inline |
Number of bits in a type or value.
Definition at line 20 of file BitOps.h.
Referenced by allLsb(), convert(), highestSetBit(), maskLsb(), msb(), msbLsb(), positionLsb(), replicate(), replicateLsb(), rotateLeft(), rotateLeftLsb(), rotateRight(), rotateRightLsb(), shiftLeft(), shiftLeftLsb(), shiftRight(), shiftRightLsb(), signExtend(), signExtendLsb(), and Rose::StringUtility::toBinary().
|
inline |
|
inline |
Generate a value with low order bits set.
Returns a value whose n
low-order bits are set and all the other higher order bits are cleared. If n
is greater than or equal to the size of the unsigned type then the returned value has all bits set. If n
is zero then no bits are set in the return value.
|
inline |
Set or clear the low-order w
bits.
Sets or clears the low order bits of the input value without affecting any of the other bits.
Definition at line 40 of file BitOps.h.
References nBits().
Referenced by shiftLeftLsb(), and shiftRightLsb().
|
inline |
|
inline |
Combine two values based on a bit mask.
The return value has bits from a
and b
depending on the mask cond
. If the mask bit i
is set, then the return value bit i
comes from a
, otherwise it comes from b
.
Definition at line 79 of file BitOps.h.
Referenced by maskLsb(), positionLsb(), replicateLsb(), rotateLeftLsb(), rotateRightLsb(), shiftLeftLsb(), and shiftRightLsb().
|
inline |
Left shift a value.
The value src
has its bits shifted n
places toward higher order. The
highest order bits are discarded and the n
new low-order bits are either set or cleared depending on the value of b
. If n
is greater than or equal to the number of bits in src
then the return value has either all bits set or all bits cleared depending on b
.
Definition at line 91 of file BitOps.h.
References nBits().
Referenced by mask(), position(), replicate(), rotateLeft(), rotateRight(), and shiftLeftLsb().
|
inline |
Left shift part of a value without affecting the rest.
The value src
has its low-order w
bits shifted n
places toward higher order. The n
highest bits are discarded and the n
new lower order bits are either set or cleared depending on the value of b
. If n
is greater than or equal to w
then all affected bits are set or cleared, depending on b
. The bits not in the affected range are not affected and are returned.
Definition at line 108 of file BitOps.h.
References allLsb(), nBits(), select(), and shiftLeft().
Referenced by rotateLeftLsb(), and rotateRightLsb().
|
inline |
Right shift a value.
Shift all bits of the value right (to lower indices) by n
. The n
low-order bits are discarded and the new n
high-order bits are set or cleared depending on b
. If n
is greater than or equal to the size of src
then the return value has either all bits set or all bits cleared depending on b
.
Definition at line 125 of file BitOps.h.
References nBits().
Referenced by bits(), bitsLsb(), rotateLeft(), rotateRight(), shiftRightLsb(), and shiftRightSigned().
|
inline |
Right shift part of a value without affecting the rest.
The value src
has its low-order w
bits shifted right toward lower order. The n
lowest bits are discarded and the n
new higher order bits are either set or cleared depending on b
. If n
is greater than or equal to w
then all affected bits are set or cleared, depending on b
. The bits not in the affected range are not affected and are returned.
Definition at line 141 of file BitOps.h.
References allLsb(), nBits(), select(), and shiftRight().
Referenced by rotateLeftLsb(), rotateRightLsb(), and shiftRightSignedLsb().
|
inline |
Generate a single-bit mask.
Returns a value that has all bit cleared except the bit at position i
. If i
is outside the valid range of bit positions for the unsigned type, then zero is returned.
Definition at line 159 of file BitOps.h.
References shiftLeft().
|
inline |
|
inline |
Generate a mask.
Returns a value where bits least
through greatest
(inclusive) are set and all other bits are cleared. The specified indexes must be valid for the type of x
. In other words, greatest
must be less than the number of bits in x
and greatest
must be greater than or equal to least
.
Definition at line 182 of file BitOps.h.
References shiftLeft().
|
inline |
|
inline |
Extract a single bit.
The bit at position i
of the value src
is returned. If i
is out of range for src
then zero is returned.
Definition at line 205 of file BitOps.h.
Referenced by highestSetBit(), msb(), msbLsb(), signExtendLsb(), and Rose::StringUtility::toBinary().
|
inline |
|
inline |
Most significant bit.
Returns the most significant bit. This is the sign bit for two's complement values.
Definition at line 226 of file BitOps.h.
References bit(), and nBits().
Referenced by convertSigned(), and shiftRightSigned().
|
inline |
Most significant bit within lsb region.
Returns the value of the most significant bit within the region of w
low-order bits. If w
is zero then returns false.
Definition at line 236 of file BitOps.h.
References bit(), and nBits().
Referenced by shiftRightSignedLsb(), and signExtend().
|
inline |
Right shift replicating MSB.
Shift all bits of the value right (to lower indices) by n
. The n
low-order bits are discarded and the new n
high-order bits are set or cleared depending on the original most significant bit. If n
is greater than or equal to the size of src
then the return value has either all bits set or all bits cleared depending on its original most significant bit.
Definition at line 250 of file BitOps.h.
References msb(), and shiftRight().
|
inline |
Right shift low bits without affecting other bits.
Right shift the low-order w
bits by n
without affecting higher order bits. The width, w
, must not be larger than the src
width. If
is greater than or equal to w
then the w
low order bits are set or cleared depending on bit w-1
. Otherwise, the n
low order bits are discarded and the n
new bits introduced at index w-1
are either zero or one depending on bit w-1
.
Definition at line 263 of file BitOps.h.
References msbLsb(), and shiftRightLsb().
|
inline |
Extract part of a value.
Extracts the bits in the range least
through greatest
(inclusive) and shifts them right by least
bits. The least
and greatest
indices must be valid for src
as defined by mask.
Definition at line 274 of file BitOps.h.
References shiftRight().
|
inline |
Extract part of a value limited by width.
Extracts the bits in the range least
through greatest
(inclusive) and shifts them right by least
bits. Any bits of src
at index w
or greater are treated as zeros.
Definition at line 285 of file BitOps.h.
References shiftRight().
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Rotate bits left.
Rotates the bits of src
left (toward higher indices) by n
bits. This is similar to shiftLeft except the high order bits that would normally be discarded are reintroduced in the low order positions. If n
is zero then this is a no-op. The rotation amount is calculated modulo the width of src
Definition at line 366 of file BitOps.h.
References nBits(), shiftLeft(), and shiftRight().
|
inline |
Rotate low-order bits left without affecting others.
Rotates the low-order w
bits of src
left by n
bits without affecting the other bits, and returns the result. The rotation amount is modulo w
. If w
is zero then the original value is returned.
Definition at line 378 of file BitOps.h.
References nBits(), select(), shiftLeftLsb(), and shiftRightLsb().
|
inline |
Rotate bits right.
Rotates the bits of src
right (toward lower indices) by n
bits. This is similar to shiftRight except the low order bits that would normally be discarded are reintroduced in the high order positions. If n
is zero then this is a no-op. The rotation amount is calculated modulo the width of src
.
Definition at line 394 of file BitOps.h.
References nBits(), shiftLeft(), and shiftRight().
|
inline |
Rotate low-order bits right without affecting others.
Rotates the low-order w
bits of src
right by n
bits without affecting the higher-order bits, and returns the result. The rotation amount is modulo w
. If w
is zero then the original value is returned.
Definition at line 406 of file BitOps.h.
References nBits(), select(), shiftLeftLsb(), and shiftRightLsb().
|
inline |
Replicate low-order bits to fill return value.
The n
low-order bits of src
are repeated as a group as many times as necessary to fill the entire return value. For instance, if src
contains 0xabcdef and n
is 8 and the return type is a 32-bit unsigned integer, then the return value will be 0xefefefef. If the width of the return value is not an integer multiple of n
, then the high order bits of the return value will contain only some of the lowest order bits of the src
. The value of n
cannot be zero.
Definition at line 423 of file BitOps.h.
References nBits(), and shiftLeft().
Referenced by replicateLsb().
|
inline |
Replicate low-order bits to fill region without affecting other bits.
This is identical to replicate except that instead of filling the entire return value with the replicated bits, at most w
low-order bits of the return value are filled with replicated bits and the remaining high order bits are copied from src
.
Definition at line 444 of file BitOps.h.
References nBits(), replicate(), and select().
|
inline |
|
inline |
|
inline |
Reverse the bytes.
Definition at line 481 of file BitOps.h.
Referenced by fromBigEndian(), fromLittleEndian(), Rose::BinaryAnalysis::ByteOrder::swapBytes(), toBigEndian(), and toLittleEndian().
|
inline |
True if host is big endian.
Definition at line 505 of file BitOps.h.
Referenced by fromBigEndian(), and toBigEndian().
|
inline |
True if host is little endian.
Definition at line 514 of file BitOps.h.
Referenced by fromLittleEndian(), and toLittleEndian().
|
inline |
Convert integral value from host order to big endian.
Definition at line 524 of file BitOps.h.
References isBigEndian(), and reverseBytes().
Referenced by Rose::BinaryAnalysis::ByteOrder::hostToBe(), and Rose::BinaryAnalysis::ByteOrder::hostToDisk().
|
inline |
Convert integral value from host order to little endian.
Definition at line 534 of file BitOps.h.
References isLittleEndian(), and reverseBytes().
Referenced by Rose::BinaryAnalysis::ByteOrder::hostToDisk(), and Rose::BinaryAnalysis::ByteOrder::hostToLe().
|
inline |
Convert integral value from big endian to host order.
Definition at line 544 of file BitOps.h.
References isBigEndian(), and reverseBytes().
Referenced by Rose::BinaryAnalysis::ByteOrder::beToHost(), and Rose::BinaryAnalysis::ByteOrder::diskToHost().
|
inline |
Convert integral value from little endian to host order.
Definition at line 554 of file BitOps.h.
References isLittleEndian(), and reverseBytes().
Referenced by Rose::BinaryAnalysis::ByteOrder::diskToHost(), and Rose::BinaryAnalysis::ByteOrder::leToHost().