byte and short data types use cases

Murloc justanotheraccount3212334 at proton.me
Fri Jun 9 11:24:38 UTC 2023


Hi, I was interested why, for example, `byte` and `short` 
literals do not have their own unique suffixes (like `L` for 
`long` or `u` for `unsigned int` literals) and found the 
following explanation:

- "I guess short literal is not supported solely due to the fact 
that anything less than `int` will be "promoted" to `int` during 
evaluation. `int` has the most natural size. This is called 
integer promotion in C++."

Which raised another question: since objects of types smaller 
than `int` are promoted to `int` to use integer arithmetic on 
them anyway, is there any point in using anything of integer type 
less than `int` other than to limit the range of values that can 
be assigned to a variable at compile time? Are these data types 
there because of some historical reasons (maybe `byte` and/or 
`short` were "natural" for some architectures before)?

People say that there is no advantage for using `byte`/`short` 
type for integer objects over an int for a single variable, 
however, as they say, this is not true for arrays, where you can 
save some memory space by using `byte`/`short` instead of `int`. 
But isn't any further manipulations with these array objects will 
produce results of type `int` anyway? Don't you have to cast 
these objects over and over again after manipulating them to 
write them back into that array or for some other manipulations 
with these smaller types objects? Or is this only useful if 
you're storing some array of constants for reading purposes?

Some people say that these promoting and casting operations in 
summary may have an even slower overall effect than simply using 
int, so I'm kind of confused about the use cases of these data 
types... (I think that my misunderstanding comes from not knowing 
how things happen at a slightly lower level of abstractions, like 
which operations require memory allocation, which do not, etc. 
Maybe some resource recommendations on that?) Thanks!


More information about the Digitalmars-d-learn mailing list