Skip to content
DryPerspective edited this page Sep 24, 2023 · 3 revisions

Class dp::byte models std::byte - an object which is exactly one byte in size and supports bitwise, but not arithmetic, operations. This can be used when performing bitwise operations while ensuring that no implicit conversions between base types will happen.

Note that without scoped enums as the standard uses, it is not possible to implement a truly portable type which is guaranteed to be one byte in size and which does not suffer from arithmetic conversions. It is possible, but very unlikely, that the compiler will pad out the class. We also cannot inherit from or composite a static assertion class because one of the compilers this library was tested on pads empty classes up to eight bytes (don't ask me why) which would instantly break the size guarantee. As such, we take the approach of a free static assertion in the header. This is suboptimal, but guarantees that all possible uses of dp::byte will either be one byte large or fail compilation. As such, dp::byte is not used internally elsewhere in the library to ensure that its use on an exotic implementation does not also forbid unrelated code.

Features

byte A type representing a byte
to_integer Converts a byte to an integer value of a given type
operator<<=
operator>>=
operator<<
operator>>
Bitwise left- and right- shift operations on a byte
operator|=
operator|
Bitwise OR operations on a byte
operator&=
operator&
Bitwise AND operations on a byte
operator^=
operator^
Bitwise XOR operations on a byte

Sample code

#include "cpp98/byte.h"

int main(){
    dp::byte b(5);    //0000 0101
    b <<= 3;          //0010 1000
    b |= dp::byte(3); //0010 1011
    Print(dp::to_integer<int>(b)); //Prints 43
}

Clone this wiki locally