1#ifndef _CPG_BITWISE_HPP
2#define _CPG_BITWISE_HPP
32 static const bool value =
false;
59 template<
typename T>
constexpr T
limits_max = std::numeric_limits<T>::max();
60 template<
typename T>
constexpr T
limits_min = std::numeric_limits<T>::min();
72 template<
typename T,
typename = enable_if_
integral<T>>
75 template<
typename T,
typename = enable_if_
integral<T>>
78 template<
typename S,
typename T>
81 template<
typename S,
typename T,
86 template<
typename S,
typename T,
98 template<std::
integral T>
105 for (; mask; mask >>= 1)
106 count += (
int)(mask & 1);
111 template<std::
integral T>
117 std::ostringstream os;
119 for (
int pos = sizeof_bits<T>; pos; mask <<= 1, --pos)
121 if (!os.str().empty() && (pos % 4 == 0))
124 os << ((mask & high_bit_mask<T>) ?
'1' :
'0');
130 template<std::
integral T, std::
size_t N>
133 std::ostringstream os;
135 for(
int n = (
int)N-1; n > 0 ; --n)
137 os <<
to_bits(bits[n]) <<
" | ";
145 template<std::
integral T, std::
size_t N>
148 std::ostringstream os;
150 for(
int n = 0; n < (int)N - 1; ++n)
152 os <<
to_bits(bits[n]) <<
" | ";
160 template<std::
integral T>
167 std::ostringstream os;
169 os << std::setfill(
'0') << std::setw(
sizeof(T) * 2)
170 << std::uppercase << std::hex << (short)v;
185 ss.push_back(str[2]);
186 ss.push_back(str[3]);
194 std::ostringstream os;
196 os << std::setfill(
'0') << std::setw(
sizeof(T) * 2)
197 << std::uppercase << std::hex <<v;
205 template<std::
integral T, std::
size_t N>
208 std::ostringstream os;
210 for(
int n = (
int)N-1; n > 0 ; --n)
212 os <<
to_hex(v[n]) <<
" | ";
220 template<std::
integral T, std::
size_t N>
223 std::ostringstream os;
225 for(
int n = 0; n < (int)N - 1 ; ++n)
227 os <<
to_hex(v[n]) <<
" | ";
235 template<std::
integral T>
238 std::ostringstream os;
248 template<std::
integral T, std::
size_t N>
251 std::ostringstream os;
253 for(
int n = (
int)N-1; n > 0; --n)
255 os <<
to_dec(v[n]) <<
" | ";
263 template<std::
integral T, std::
size_t N>
266 std::ostringstream os;
268 for(
int n = 0; n < (int)N - 1; ++n)
270 os <<
to_dec(v[n]) <<
" | ";
278 template<std::
integral T>
281 std::ostringstream os;
288 return (
int) os.str().size();
291 template<std::
integral T>
294 int a =
field_with(std::numeric_limits<T>::max());
295 int b =
field_with(std::numeric_limits<T>::min());
297 return a > b ? a : b;
300 template<std::
integral T>
303 std::ostringstream os;
305 int max_field = numeric_width<T>();
308 os << std::setw(max_field)<<(short)v;
310 os << std::setw(max_field)<<v;
315 template<std::
integral T>
318 std::ostringstream os;
320 os <<
to_dec_width(v) <<
" (" << to_hex<T>(v) <<
"): " << to_bits<T>(v);
325 template<std::
integral T>
328 std::ostringstream os;
330 auto minimum = std::numeric_limits<T>::min();
331 auto maximum = std::numeric_limits<T>::max();
334 <<
",\tByte size: " <<
sizeof(T) <<
",\tBit count: " << sizeof_bits<T>
341 template<
typename =
void>
344 std::ostringstream os;
346 os << numeric_type_info<char>() <<
"\n\n";
347 os << numeric_type_info<unsigned char>() <<
"\n\n";
348 os << numeric_type_info<short>() <<
"\n\n";
349 os << numeric_type_info<unsigned short>() <<
"\n\n";
350 os << numeric_type_info<int>() <<
"\n\n";
351 os << numeric_type_info<unsigned int>() <<
"\n\n";
352 os << numeric_type_info<long>() <<
"\n\n";
353 os << numeric_type_info<unsigned long>() <<
"\n\n";
354 os << numeric_type_info<long long>() <<
"\n\n";
355 os << numeric_type_info<unsigned long long>() <<
"\n";
360 template<std::
integral T>
363 std::ostringstream os;
364 os << numeric_type_info<T>() <<
"\n";
369 os <<
"original value : c = " <<
numeric_base(c) <<
"\n";
370 os <<
"1's complement : ~c = " <<
numeric_base(c1) <<
"\n";
371 os <<
"2's complement : ~c + 1 = " <<
numeric_base(c2) <<
"\n";
372 os <<
"complement check: c + (~c+1) = " <<
numeric_base(c3) <<
"\n";
377 template<std::
unsigned_
integral T>
381 constexpr static T
min_limit = std::numeric_limits<T>::min();
382 constexpr static T
max_limit = std::numeric_limits<T>::max();
398 const std::byte&
msb() const noexcept
410 const std::byte&
lsb() const noexcept
422 requires std::floating_point<S> || std::integral<S>
425 if constexpr(std::floating_point<S>)
428 this->
n = (T) std::round(s);
441 requires std::floating_point<S> || std::integral<S>
442 operator S() const noexcept
450#define Tpf_SignedCommonType(a, b) tpf::bitwise::signed_common_t<decltype(a), decltype(b)>
451#define Tpf_UnignedCommonType(a, b) tpf::bitwise::unsigned_common_t<decltype(a), decltype(b)>
#define Cpg_GetTypeName(type_arg,...)
std::enable_if_t< std::is_integral< T >::value > enable_if_integral
constexpr int sizeof_bits
signed_t< common_t< S, T > > signed_common_t
std::string to_hex_reverse(T(&v)[N])
std::string twos_complement(T c)
std::string to_bits_reverse(T(&bits)[N])
int count_set_bits(T bits)
std::string to_dec_width(T v)
unsigned_t< common_t< S, T > > unsigned_common_t
std::common_type_t< S, T > common_t
std::make_signed_t< T > signed_t
std::string numeric_type_info()
std::string integral_type_info()
constexpr unsigned_t< T > high_bit_mask
std::string to_dec_reverse(T(&v)[N])
std::string numeric_base(T v)
std::make_unsigned_t< T > unsigned_t
std::string to_bits(T bits)
auto minimum(Type_1 a, Type_2 b)
auto maximum(Type_1 a, Type_2 b)
static constexpr size_t bit_size
std::byte & msb() noexcept
static constexpr size_t byte_size
static constexpr T max_limit
const std::byte & msb() const noexcept
const std::byte & lsb() const noexcept
std::byte & lsb() noexcept
static constexpr T min_limit
bit_pattern & operator=(S s) noexcept