C++ Standard Library C++ STL Library

C++ <cstddef> - byte Type



The C++ <cstddef> byte is a distinct type that implements the concept of byte as specified in the C++ language definition.

Like char and unsigned char, it can be used to access raw memory occupied by other objects, but unlike those types, it is not a character type and is not an arithmetic type. A byte is only a collection of bits, and the only operators defined for it are the bitwise operators.

In the <cstddef> header file, it is defined as follows:

enum class byte : unsigned char {} ;

Example:

The example below shows the usage of byte type.

#include <iostream>
#include <cstddef>
#include <bitset>
using namespace std;
 
ostream& operator<< (ostream& os, byte b) {
  return os<<bitset<8>(to_integer<int>(b));
}
 
int main() {
  byte b{100};
  cout<<"1. "<<b<<"\n";

  b <<= 1;
  cout<<"2. "<<b<<"\n";

  b >>= 1;
  cout<<"3. "<<b << "\n";

  cout<<"4. "<<(b << 1)<<"\n";
  cout<<"5. "<<(b >> 1)<<"\n";

  b |= byte{0b11110000};
  cout<<"6. "<<b<<"\n";

  b &= byte{0b11110000};
  cout<<"7. "<<b<<"\n";

  b ^= byte{0b11111111};
  cout<<"8. "<<b<<"\n";
}

The output of the above code will be:

1. 01100100
2. 11001000
3. 01100100
4. 11001000
5. 00110010
6. 11110100
7. 11110000
8. 00001111

Non-member functions

to_integer

template <class IntegerType>
 constexpr IntegerType to_integer(byte b) noexcept;

Equivalent to: return IntegerType(b). 
This overload participates in overload resolution only if is_integral_v<IntegerType> is true. 

operator<<=, operator>>=

template <class IntegerType>
 constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept;
template <class IntegerType>
 constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept;

1) Equivalent to: return b = b << shift; 
   This overload participates in overload resolution only if is_integral_v<IntegerType> is true.
2) Equivalent to: return b = b >> shift;
   This overload participates in overload resolution only if is_integral_v<IntegerType> is true. 

operator<<, operator>>

template <class IntegerType>
 constexpr byte operator <<(byte b, IntegerType shift) noexcept;   (1)
template <class IntegerType>
 constexpr byte operator >>(byte b, IntegerType shift) noexcept;   (2)

1) Equivalent to: return byte(static_cast<unsigned int>(b) << shift);
   This overload participates in overload resolution only if is_integral_v<IntegerType> is true.
2) Equivalent to: return byte(static_cast<unsigned int>(b) >> shift); 
   This overload participates in overload resolution only if is_integral_v<IntegerType> is true.

operator|=, operator&=, operator^=

constexpr byte& operator|=(byte& l, byte r) noexcept;   (1)  
constexpr byte& operator&=(byte& l, byte r) noexcept;   (2)
constexpr byte& operator^=(byte& l, byte r) noexcept;   (3)

1) Equivalent to: return l = l | r;
2) Equivalent to: return l = l & r;
3) Equivalent to: return l = l ^ r;

operator|, operator&, operator^, operator~

constexpr byte operator|(byte l, byte r) noexcept;   (1)
constexpr byte operator&(byte l, byte r) noexcept;   (2)
constexpr byte operator^(byte l, byte r) noexcept;   (3)
constexpr byte operator~(byte b) noexcept;           (4)

1) Equivalent to: return byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));
2) Equivalent to: return byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));
3) Equivalent to: return byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));
4) Equivalent to: return byte(~static_cast<unsigned int>(b));


❮ C++ <cstddef> Library