Integer promotion issue

Evan maskedmonkyman at
Sat Jan 25 23:46:29 UTC 2020

I am writing an 8 bit emulator that uses a lot of small numbers 
and running into a problem where the compiler will automatically 
promote smaller operands up to an int which causes the result of 
math calculations to not fit into smaller types.

//compiled with dmd 2.090.0
void main()
     ubyte a = 0x01;
     ubyte b = 0x01;
     ubyte c = a + b; //Error: cannot implicitly convert 
expression cast(int)a + cast(int)b of type int to ubyte

Is there a way to stop the compiler from up casting 
automatically? or have it down cast it back once it's done?

More information about the Digitalmars-d mailing list