Specifying bit size of integer literals

OK I can do 98l and force the compiler to make the value 98 a long integer.

How do I force the compiler to make 89 a byte? What is the correct letter?

Also given this: typedef enum{eNOTHING, eCLICKED, eHELD, eDOUBLECLICKED} EButtonActivity;

Is there any way you can force the enumerated values to byte size rather than the default integer size?

Byte doesn't have a suffix like long does. You could add one yourself, but it's not all that useful, because integers with a size smaller than "int" are promoted to "int" when using built-in operators. It could be useful for overload resolution, though, or for use in the capture list of a lambda.

uint8_t operator""_b(unsigned long value) {
  return value;
}

auto myByte = 255_b; // type of myByte will be "uint8_t" aka "byte"
auto sumOfBytes = 1_b + 1_b; // type of the operands is promoted to "int", so type of sumOfBytes is "int" as well.

You're using C syntax for your enum, use the C++ syntax instead, and use a colon to specify the type. Always use scoped enums, otherwise you'll have name collisions, especially with generic names like "nothing".

https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations

enum class EButtonActivity : uint8_t {
  NOTHING, CLICKED, HELD, DOUBLECLICKED,
};

Pieter

PieterP:
Byte doesn't have a suffix like long does. You could add one yourself, but it's not all that useful, because integers with a size smaller than "int" are promoted to "int" when using built-in operators. It could be useful for overload resolution, though, or for use in the capture list of a lambda.

uint8_t operator""_b(unsigned long value) {

return value;
}

auto myByte = 255_b; // type of myByte will be "uint8_t" aka "byte"
auto sumOfBytes = 1_b + 1_b; // type of the operands is promoted to "int", so type of sumOfBytes is "int" as well.




You're using C syntax for your enum, use the C++ syntax instead, and use a colon to specify the type. Always use scoped enums, otherwise you'll have name collisions, especially with generic names like "nothing".

https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations



enum class EButtonActivity : uint8_t {
  NOTHING, CLICKED, HELD, DOUBLECLICKED,
};




Pieter

Oh cool. Had no clue about the C++ version of enums. Thanks for that.