Difference between revisions of "cpp/numeric/byteswap"
From cppreference.com
(→Possible implementation: cool impl, upgrading reverse to ranges::reverse to further increase coolness) |
m (→Notes: ~FTM) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
}} | }} | ||
− | Reverses the bytes in the given integer value {{ | + | Reverses the bytes in the given integer value {{c|n}}. |
{{tt|std::byteswap}} participates in overload resolution only if {{tt|T}} satisfies {{lconcept|integral}}, i.e., {{tt|T}} is an integer type. The program is ill-formed if {{tt|T}} has padding bits. | {{tt|std::byteswap}} participates in overload resolution only if {{tt|T}} satisfies {{lconcept|integral}}, i.e., {{tt|T}} is an integer type. The program is ill-formed if {{tt|T}} has padding bits. | ||
Line 12: | Line 12: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | n | integer value}} | + | {{par|n|integer value}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | An integer value of type {{tt|T}} whose object representation comprises the bytes of that of {{ | + | An integer value of type {{tt|T}} whose object representation comprises the bytes of that of {{c|n}} in reversed order. |
===Notes=== | ===Notes=== | ||
This function is useful for processing data of different endianness. | This function is useful for processing data of different endianness. | ||
− | + | {{feature test macro|__cpp_lib_byteswap|std=C++23|value=202110L|{{tt|std::byteswap}}}} | |
− | {{feature test macro|__cpp_lib_byteswap}} | + | |
− | + | ||
===Possible implementation=== | ===Possible implementation=== | ||
{{eq fun | {{eq fun | ||
− | + | |1= | |
template<std::integral T> | template<std::integral T> | ||
constexpr T byteswap(T value) noexcept | constexpr T byteswap(T value) noexcept | ||
{ | { | ||
− | static_assert(std::has_unique_object_representations_v<T>, "T may not have padding bits"); | + | static_assert(std::has_unique_object_representations_v<T>, |
+ | "T may not have padding bits"); | ||
auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value); | auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value); | ||
std::ranges::reverse(value_representation); | std::ranges::reverse(value_representation); | ||
Line 40: | Line 39: | ||
{{example|code= | {{example|code= | ||
#include <bit> | #include <bit> | ||
− | |||
#include <concepts> | #include <concepts> | ||
− | #include < | + | #include <cstdint> |
#include <iomanip> | #include <iomanip> | ||
+ | #include <iostream> | ||
− | template <std::integral T> | + | template<std::integral T> |
− | void dump(T v, char term = '\n') { | + | void dump(T v, char term = '\n') |
+ | { | ||
std::cout << std::hex << std::uppercase << std::setfill('0') | std::cout << std::hex << std::uppercase << std::setfill('0') | ||
<< std::setw(sizeof(T) * 2) << v << " : "; | << std::setw(sizeof(T) * 2) << v << " : "; | ||
− | for (std::size_t i{}; i != sizeof(T); ++i, v >>= 8) | + | for (std::size_t i{}; i != sizeof(T); ++i, v >>= 8) |
std::cout << std::setw(2) << static_cast<unsigned>(T(0xFF) & v) << ' '; | std::cout << std::setw(2) << static_cast<unsigned>(T(0xFF) & v) << ' '; | ||
− | |||
std::cout << std::dec << term; | std::cout << std::dec << term; | ||
} | } | ||
Line 74: | Line 73: | ||
dump(std::byteswap(z)); | dump(std::byteswap(z)); | ||
} | } | ||
− | + | |p=true | |
− | + | |output= | |
byteswap for U16: | byteswap for U16: | ||
CAFE : FE CA | CAFE : FE CA | ||
Line 91: | Line 90: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/types/dsc endian}} | + | {{dsc inc|cpp/types/dsc endian}} |
− | {{dsc inc | cpp/numeric/dsc rotl}} | + | {{dsc inc|cpp/numeric/dsc rotl}} |
− | {{dsc inc | cpp/numeric/dsc rotr}} | + | {{dsc inc|cpp/numeric/dsc rotr}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 09:30, 24 July 2023
Defined in header <bit>
|
||
template< class T > constexpr T byteswap( T n ) noexcept; |
(since C++23) | |
Reverses the bytes in the given integer value n.
std::byteswap
participates in overload resolution only if T
satisfies integral
, i.e., T
is an integer type. The program is ill-formed if T
has padding bits.
Contents |
[edit] Parameters
n | - | integer value |
[edit] Return value
An integer value of type T
whose object representation comprises the bytes of that of n in reversed order.
[edit] Notes
This function is useful for processing data of different endianness.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_byteswap |
202110L | (C++23) | std::byteswap
|
[edit] Possible implementation
template<std::integral T> constexpr T byteswap(T value) noexcept { static_assert(std::has_unique_object_representations_v<T>, "T may not have padding bits"); auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value); std::ranges::reverse(value_representation); return std::bit_cast<T>(value_representation); } |
[edit] Example
Run this code
#include <bit> #include <concepts> #include <cstdint> #include <iomanip> #include <iostream> template<std::integral T> void dump(T v, char term = '\n') { std::cout << std::hex << std::uppercase << std::setfill('0') << std::setw(sizeof(T) * 2) << v << " : "; for (std::size_t i{}; i != sizeof(T); ++i, v >>= 8) std::cout << std::setw(2) << static_cast<unsigned>(T(0xFF) & v) << ' '; std::cout << std::dec << term; } int main() { static_assert(std::byteswap('a') == 'a'); std::cout << "byteswap for U16:\n"; constexpr auto x = std::uint16_t(0xCAFE); dump(x); dump(std::byteswap(x)); std::cout << "\nbyteswap for U32:\n"; constexpr auto y = std::uint32_t(0xDEADBEEFu); dump(y); dump(std::byteswap(y)); std::cout << "\nbyteswap for U64:\n"; constexpr auto z = std::uint64_t{0x0123456789ABCDEFull}; dump(z); dump(std::byteswap(z)); }
Possible output:
byteswap for U16: CAFE : FE CA FECA : CA FE byteswap for U32: DEADBEEF : EF BE AD DE EFBEADDE : DE AD BE EF byteswap for U64: 0123456789ABCDEF : EF CD AB 89 67 45 23 01 EFCDAB8967452301 : 01 23 45 67 89 AB CD EF
[edit] See also
(C++20) |
indicates the endianness of scalar types (enum) |
(C++20) |
computes the result of bitwise left-rotation (function template) |
(C++20) |
computes the result of bitwise right-rotation (function template) |