Search code examples
c++ccompiler-constructionprogramming-languageslanguage-design

What is a good way of working with variables in 8 bit blocks?


I'm playing around with a toy language design of mine and I have a few problems. I would like it to have variables specified in bit length. So, for example, one declares the variables like so:

byte value;
two_bytes value;

Now, here's my problem. I'm trying to make an interpreter for the language in C (or C++). My understanding of C/C++ is that their variable types are guaranteed to be at least a minimum size, but they can be larger (ie, a char will be at least 8 bits, but some implementations will have a 16 bit char).

How can I write an interpreter in C/C++ that deals only with specific lengths of bits? Is the only way to have an array of booleans or to set up bitfields for something like the char type?


Solution

  • stdint header is what you need:

    #include <cstdint>
    std::int32_t x; // 32 bits signed
    std::uint16_t y; // 16 bits unsigned