tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
byte.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CSTDDEF_BYTE_HPP
5#define TETL_CSTDDEF_BYTE_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_concepts/integral.hpp>
10
11namespace etl {
12
13/// \brief etl::byte is a distinct type that implements the concept of byte as
14/// specified in the C++ language definition.
15///
16/// \details Like char and unsigned char, it can be used to access raw memory
17/// occupied by other objects, but unlike those types, it is not a character
18/// type and is not an arithmetic type. A byte is only a collection of bits, and
19/// the only operators defined for it are the bitwise ones.
20///
21/// https://en.cppreference.com/w/cpp/types/byte
22enum struct TETL_MAY_ALIAS byte : unsigned char {
23};
24
25/// \brief Equivalent to: `return Int(b);`
26template <etl::integral Int>
27[[nodiscard]] constexpr auto to_integer(etl::byte b) noexcept -> Int
28{
29 return static_cast<Int>(b);
30}
31
32/// \brief Equivalent to: `return etl::byte(static_cast<unsigned int>(b) << shift);`
33template <etl::integral Int>
34[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator<<(etl::byte b, Int shift) noexcept -> etl::byte
35{
36 return etl::byte(static_cast<unsigned int>(b) << shift);
37}
38
39/// \brief Equivalent to: `return etl::byte(static_cast<unsigned int>(b) >> shift);`
40template <etl::integral Int>
41[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator>>(etl::byte b, Int shift) noexcept -> etl::byte
42{
43 return etl::byte(static_cast<unsigned int>(b) >> shift);
44}
45
46/// \brief Equivalent to: `return b = b << shift;`
47template <etl::integral Int>
48TETL_ALWAYS_INLINE constexpr auto operator<<=(etl::byte& b, Int shift) noexcept -> etl::byte&
49
50{
51 return b = b << shift;
52}
53
54/// \brief Equivalent to: `return b = b >> shift;`
55template <etl::integral Int>
56TETL_ALWAYS_INLINE constexpr auto operator>>=(etl::byte& b, Int shift) noexcept -> etl::byte&
57{
58 return b = b >> shift;
59}
60
61/// \brief Equivalent to: `return byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));`
62[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator|(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
63{
64 return etl::byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
65}
66
67/// \brief Equivalent to: `return byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));`
68[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator&(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
69{
70 return etl::byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));
71}
72
73/// \brief Equivalent to: `return byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));`
74[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator^(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
75{
76 return etl::byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
77}
78
79/// \brief Equivalent to: `return byte(~static_cast<unsigned int>(b));`
80[[nodiscard]] TETL_ALWAYS_INLINE constexpr auto operator~(etl::byte b) noexcept -> etl::byte
81{
82 return etl::byte(~static_cast<unsigned int>(b));
83}
84
85/// \brief Equivalent to: `return lhs = lhs | rhs;`
86TETL_ALWAYS_INLINE constexpr auto operator|=(etl::byte& lhs, etl::byte rhs) noexcept -> etl::byte&
87{
88 return lhs = lhs | rhs;
89}
90
91/// \brief Equivalent to: `return lhs = lhs & rhs;`
92TETL_ALWAYS_INLINE constexpr auto operator&=(etl::byte& lhs, etl::byte rhs) noexcept -> etl::byte&
93{
94 return lhs = lhs & rhs;
95}
96
97/// \brief Equivalent to: `return lhs = lhs ^ rhs;`
98TETL_ALWAYS_INLINE constexpr auto operator^=(etl::byte& lhs, etl::byte rhs) noexcept -> etl::byte&
99{
100 return lhs = lhs ^ rhs;
101}
102
103} // namespace etl
104
105#endif // TETL_CSTDDEF_BYTE_HPP
Definition adjacent_find.hpp:9
constexpr auto to_integer(etl::byte b) noexcept -> Int
Equivalent to: return Int(b);
Definition byte.hpp:27
TETL_ALWAYS_INLINE constexpr auto operator^(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
Equivalent to: return byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
Definition byte.hpp:74
TETL_ALWAYS_INLINE constexpr auto operator<<(etl::byte b, Int shift) noexcept -> etl::byte
Equivalent to: return etl::byte(static_cast<unsigned int>(b) << shift);
Definition byte.hpp:34
TETL_ALWAYS_INLINE constexpr auto operator>>(etl::byte b, Int shift) noexcept -> etl::byte
Equivalent to: return etl::byte(static_cast<unsigned int>(b) >> shift);
Definition byte.hpp:41
TETL_ALWAYS_INLINE constexpr auto operator>>=(etl::byte &b, Int shift) noexcept -> etl::byte &
Equivalent to: return b = b >> shift;
Definition byte.hpp:56
TETL_ALWAYS_INLINE constexpr auto operator^=(etl::byte &lhs, etl::byte rhs) noexcept -> etl::byte &
Equivalent to: return lhs = lhs ^ rhs;
Definition byte.hpp:98
TETL_ALWAYS_INLINE constexpr auto operator&=(etl::byte &lhs, etl::byte rhs) noexcept -> etl::byte &
Equivalent to: return lhs = lhs & rhs;
Definition byte.hpp:92
TETL_ALWAYS_INLINE constexpr auto operator&(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
Equivalent to: return byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));
Definition byte.hpp:68
TETL_ALWAYS_INLINE constexpr auto operator|=(etl::byte &lhs, etl::byte rhs) noexcept -> etl::byte &
Equivalent to: return lhs = lhs | rhs;
Definition byte.hpp:86
TETL_ALWAYS_INLINE constexpr auto operator|(etl::byte lhs, etl::byte rhs) noexcept -> etl::byte
Equivalent to: return byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
Definition byte.hpp:62
enum TETL_MAY_ALIAS byte
etl::byte is a distinct type that implements the concept of byte as specified in the C++ language def...
Definition byte.hpp:22
TETL_ALWAYS_INLINE constexpr auto operator~(etl::byte b) noexcept -> etl::byte
Equivalent to: return byte(~static_cast<unsigned int>(b));
Definition byte.hpp:80
TETL_ALWAYS_INLINE constexpr auto operator<<=(etl::byte &b, Int shift) noexcept -> etl::byte &
Equivalent to: return b = b << shift;
Definition byte.hpp:48