1#ifndef _TPF_BITWISE_HPP
2#define _TPF_BITWISE_HPP
4#ifndef TBB_SUPPRESS_DEPRECATED_MESSAGES
5 #define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
28 static const bool value =
false;
55 template<
typename T>
constexpr T
limits_max = std::numeric_limits<T>::max();
56 template<
typename T>
constexpr T
limits_min = std::numeric_limits<T>::min();
68 template<
typename T,
typename = enable_if_
integral<T>>
71 template<
typename T,
typename = enable_if_
integral<T>>
74 template<
typename S,
typename T>
77 template<
typename S,
typename T,
82 template<
typename S,
typename T,
94 template<
typename T,
typename = enable_if_
integral<T>>
101 for (; mask; mask >>= 1)
102 count += (
int)(mask & 1);
107 template<
typename T,
typename = enable_if_
integral<T>>
112 int count = 0; std::string str;
114 for (
int pos = sizeof_bits<T>; pos; mask <<= 1, --pos)
116 if (!str.empty() && (pos % 4 == 0))
119 str.push_back((mask & high_bit_mask<T>) ?
'1' :
'0');
125 template<
typename T,
typename = enable_if_
integral<T>>
132 std::ostringstream os;
134 os << std::setfill(
'0') << std::setw(
sizeof(T) * 2)
135 << std::uppercase << std::hex << (short)v;
150 ss.push_back(str[2]);
151 ss.push_back(str[3]);
159 std::ostringstream os;
161 os << std::setfill(
'0') << std::setw(
sizeof(T) * 2)
162 << std::uppercase << std::hex <<v;
170 template<
typename T, std::
size_t N>
173 std::ostringstream os;
175 for(
int n = (
int)N-1; n > 0 ; --n)
177 os <<
to_hex(v[n]) <<
" | ";
185 template<
typename T, std::
size_t N>
188 std::ostringstream os;
190 for(
int n = 0; n < (int)N - 1 ; ++n)
192 os <<
to_hex(v[n]) <<
" | ";
200 template<
typename T,
typename = enable_if_
integral<T>>
203 std::ostringstream os;
213 template<
typename T, std::
size_t N>
216 std::ostringstream os;
218 for(
int n = (
int)N-1; n > 0; --n)
220 os <<
to_dec(v[n]) <<
" | ";
228 template<
typename T, std::
size_t N>
231 std::ostringstream os;
233 for(
int n = 0; n < (int)N - 1; ++n)
235 os <<
to_dec(v[n]) <<
" | ";
243 template<
typename T,
typename = enable_if_
integral<T>>
246 std::ostringstream os;
253 return (
int) os.str().size();
256 template<
typename T,
typename = enable_if_
integral<T>>
259 int a =
field_with(std::numeric_limits<T>::max());
260 int b =
field_with(std::numeric_limits<T>::min());
262 return a > b ? a : b;
265 template<
typename T,
typename = enable_if_
integral<T>>
268 std::ostringstream os;
270 int max_field = numeric_width<T>();
273 os << std::setw(max_field)<<(short)v;
275 os << std::setw(max_field)<<v;
280 template<
typename T,
typename = enable_if_
integral<T>>
283 std::ostringstream os;
285 os <<
to_dec_width(v) <<
" (" << to_hex<T>(v) <<
"): " << to_bits<T>(v);
290 template<
typename T,
typename = enable_if_
integral<T>>
293 std::ostringstream os;
295 auto minimum = std::numeric_limits<T>::min();
296 auto maximum = std::numeric_limits<T>::max();
299 <<
",\tByte size: " <<
sizeof(T) <<
",\tBit count: " << sizeof_bits<T>
306 template<
typename =
void>
309 std::ostringstream os;
311 os << numeric_type_info<char>() <<
"\n\n";
312 os << numeric_type_info<unsigned char>() <<
"\n\n";
313 os << numeric_type_info<short>() <<
"\n\n";
314 os << numeric_type_info<unsigned short>() <<
"\n\n";
315 os << numeric_type_info<int>() <<
"\n\n";
316 os << numeric_type_info<unsigned int>() <<
"\n\n";
317 os << numeric_type_info<long>() <<
"\n\n";
318 os << numeric_type_info<unsigned long>() <<
"\n\n";
319 os << numeric_type_info<long long>() <<
"\n\n";
320 os << numeric_type_info<unsigned long long>() <<
"\n";
325 template<
typename T,
typename = enable_if_
integral<T>>
328 std::ostringstream os;
329 os << numeric_type_info<T>() <<
"\n";
334 os <<
"original value : c = " <<
numeric_base(c) <<
"\n";
335 os <<
"1's complement : ~c = " <<
numeric_base(c1) <<
"\n";
336 os <<
"2's complement : ~c + 1 = " <<
numeric_base(c2) <<
"\n";
337 os <<
"complement check: c + (~c+1) = " <<
numeric_base(c3) <<
"\n";
347 constexpr static T
min_limit = std::numeric_limits<T>::min();
348 constexpr static T
max_limit = std::numeric_limits<T>::max();
364 const std::byte&
msb() const noexcept
376 const std::byte&
lsb() const noexcept
387 template<
typename S,
typename = tpf::types::enable_if_numerical_number_t<S>>
390 if constexpr(tpf::types::is_real_number_v<S>)
393 this->
n = (T) std::round(s);
405 template<
typename S,
typename = tpf::types::enable_if_numerical_number_t<S>>
406 operator S() const noexcept
414#define Tpf_SignedCommonType(a, b) tpf::bitwise::signed_common_t<decltype(a), decltype(b)>
415#define Tpf_UnignedCommonType(a, b) tpf::bitwise::unsigned_common_t<decltype(a), decltype(b)>
constexpr unsigned_t< T > high_bit_mask
std::make_signed_t< T > signed_t
std::enable_if_t< std::is_integral< T >::value > enable_if_integral
std::string numeric_base(T v)
std::string twos_complement(T c)
std::common_type_t< S, T > common_t
int count_set_bits(T bits)
std::string numeric_type_info()
std::string to_dec_width(T v)
std::string integral_type_info()
std::string to_dec_reverse(T(&v)[N])
constexpr int sizeof_bits
std::string to_hex_reverse(T(&v)[N])
std::make_unsigned_t< T > unsigned_t
std::string to_bits(T bits)
unsigned_t< common_t< S, T > > unsigned_common_t
signed_t< common_t< S, T > > signed_common_t
hidden::enable_if_unsigned_integral_t< Type, ReturnType > enable_if_unsigned_integral_t
auto minimum(Type_1 a, Type_2 b)
auto maximum(Type_1 a, Type_2 b)
std::byte & lsb() noexcept
const std::byte & lsb() const noexcept
std::byte & msb() noexcept
bit_pattern & operator=(S s) noexcept
static constexpr size_t bit_size
static constexpr T max_limit
static constexpr T min_limit
static constexpr size_t byte_size
const std::byte & msb() const noexcept
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.