Contents
#include "nonstd/bit.hpp"
#include <iostream>
using namespace nonstd;
int main()
{
std::cout
<< "Consecutive ones at the right in 0x17: " << countr_one( 0x17u )
<< "\nBit width of 0x13: " << bit_width( 0x13u ) << '\n';
}
prompt> g++ -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
Consecutive ones at the right in 0x17: 3
Bit width of 0x13: 5
bit lite is a single-file header-only library to provide the tools from the C++20 header <bit>
for use with C++98 and later. If available, the standard header is used, unless configured otherwise.
Features and properties of bit lite are ease of installation (single header), freedom of dependencies other than the standard library.
Limitations of bit lite are ... .
bit lite is distributed under the Boost Software License.
bit lite has no other dependencies than the C++ standard library.
bit lite is a single-file header-only library. Put bit.hpp
in the include folder directly into the project source tree or somewhere reachable from your project.
Contents
Documentation of standard header <bit>
Non-standard extensions
Configuration
<bit>
Depending on the compiler and C++-standard used, bit lite behaves less or more like C++20 standard <bit>
. To get an idea of the capabilities of bit lite with your configuration, look at the output of the tests, issuing bit-main.t --pass @
. For C++20 standard <bit>
, see its documentation at cppreference.
Kind | Extension type or function | Notes |
---|---|---|
Type | big_endian_type | type based on endian::big |
little_endian_type | type based on endian::little | |
native_endian_type | type based on endian::native | |
Free function | template<class T> T to_big_endian(T v) |
convert unconditionally |
template<class T, class EN> T to_big_endian(T v, EN) |
convert if EN is little_endian_type EN specifies endianness of v |
|
template<class T> T to_little_endian(T v) |
convert unconditionally |
|
template<class T, class EN> T to_little_endian(T v, EN) |
convert if EN is big_endian_type EN specifies endianness of v |
|
template<class T> T to_native_endian(T v) |
identity |
|
template<class T, class EN> T to_native_endian(T v, EN) |
convert if EN represents non-native endianness for v |
|
template<class T> T as_big_endian(T v) |
convert if native is little endian |
|
template<class T> T as_little_endian(T v) |
convert if native is big endian |
|
template<class T> T as_native_endian(T v) |
identity |
-Dbit_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cplusplus
macro correctly.
<bit>
or nonstd <bit>
At default, bit lite uses the C++20 standard header <bit>
if it is available and lets you use it via namespace nonstd
. You can however override this default and explicitly request to use the standard C++20 header or bit lite's header via the following macros.
-Dbit_CONFIG_SELECT_BIT=bit_BIT_DEFAULT
Define this to bit_BIT_STD
to select C++20 standard header <bit>
. Define this to bit_BIT_NONSTD
to select bit lite. Default is undefined, which has the same effect as defining to bit_BIT_DEFAULT
.
-Dbit_CONFIG_STRICT=1
Define this to 1
to omit the non C++20 standard extensions. Default is undefined, which has the same effect as 0
, non-strict mode.
TBD
TBD
<bit>
TBD
Interface and specification
Proposals
Articles
The test program provides information on the compiler, the C++ language and library capabilities and the tests performed.
The version of bit lite is available via tag [.version]
. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler]
, [.stdc++]
, [.stdlanguage]
and [.stdlibrary]
.
bit_cast<>(): successfully roundtrips uint64_t via double [bit.cast]
has_single_bit(): single bit yields false for no bits set [bit.pow.two]
has_single_bit(): single bit yields true for single bits set [bit.pow.two]
has_single_bit(): single bit yields false for multiple bits set [bit.pow.two]
bit_ceil(): let N be the smallest power of 2 greater than or equal to x [bit.pow.two]
bit_floor(): x == 0, 0; otherwise the maximal value y such that has_single_bit(y) is true and y <= x [bit.pow.two]
bit_width: x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded [bit.pow.two]
rotl(): r is 0, x; if r is positive, (x << r) | (x >> (N - r)); if r is negative, rotr(x, -r) [bit.rotate]
rotr(): r is 0, x; if r is positive, (x >> r) | (x << (N - r)); if r is negative, rotl(x, -r) [bit.rotate]
countl_zero(): the number of consecutive 0 bits in the value of x, starting from the most significant bit [bit.count]
countl_one(): the number of consecutive 1 bits in the value of x, starting from the most significant bit [bit.count]
countr_zero(): the number of consecutive 0 bits in the value of x, starting from the least significant bit [bit.count]
countr_one(): the number of consecutive 1 bits in the value of x, starting from the least significant bit [bit.count]
popcount(): the number of 1 bits in the value of x [bit.count]
endian: little differs from big (corner-case when all scalars have size of 1 byte) [bit.endian]
to_big_endian(): convert native, or little or big endian specified unsigned to big endian [bit.endian.extension]
to_little_endian(): convert native, or little or big endian specified unsigned to little endian [bit.endian.extension]
to_native_endian(): convert native, or little or big endian specified unsigned to native endian [bit.endian.extension]
as_big_endian(): provide native unsigned as big endian [bit.endian.extension]
as_little_endian(): provide native unsigned as little endian [bit.endian.extension]
as_native_endian(): provide native unsigned as native endian (identity) [bit.endian.extension]