M5Utility 0.0.2 git rev:5c1a751
Loading...
Searching...
No Matches
conversion.hpp File Reference

Numeric conversion. More...

#include <type_traits>
#include <cstdint>
#include <cstddef>

Go to the source code of this file.

Namespaces

namespace  m5
 Top level namespace of M5.
 

Functions

template<size_t Bits, typename T >
constexpr auto m5::utility::unsigned_to_signed (const T v) -> typename std::make_signed< T >::type
 Convert an unsigned integer of any maximum number of bits to a signed integer.
 

Detailed Description

Numeric conversion.

Function Documentation

◆ unsigned_to_signed()

template<size_t Bits, typename T >
auto m5::utility::unsigned_to_signed ( const T v) -> typename std::make_signed<T>::type
constexpr

Convert an unsigned integer of any maximum number of bits to a signed integer.

Template Parameters
BitsNumber of bits assumed by value
uint32_t u24{0x00FFFFFF};
// 24 bit unsigned int to int32_t
uint32_t s32 = unsigned_to_signed<24>(u24);
// s32 is -1 (Not 16777215)