Advertisement
Guest User

Untitled

a guest
Aug 10th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. template<integral T>
  2. inline T ByteSwapToBE(T unit) {
  3.     static_assert(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8);
  4.  
  5.     if constexpr(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__){
  6.         if constexpr (sizeof(T) == 2)
  7.             return __builtin_bswap16(unit);
  8.         else if constexpr (sizeof(T) == 4)
  9.             return __builtin_bswap32(unit);
  10.         else if constexpr (sizeof(T) == 8)
  11.             return __builtin_bswap64(unit);
  12.     }
  13.     return unit;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement