SHARE
TWEET

twos_compl_abs

a guest Jun 14th, 2017 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Custom abs(), that correctly handles most-negative values on 2's complement systems.
  2. // Requires T to be either unsigned, or convertible to unsigned.
  3.  
  4. #include <type_traits>
  5.  
  6. template<typename T, typename R = std::make_unsigned_t<T>>
  7. R twos_compl_abs(T val) {
  8.     if (val >= 0) { return val; }
  9.     else          { return static_cast<R>(val) * -1; }
  10. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top