Is there any reason why arithmetic operation on shorts and bytes return int?

monarch_dodra monarchdodra at gmail.com
Tue Dec 11 04:53:37 PST 2012


On Tuesday, 11 December 2012 at 12:24:14 UTC, d coder wrote:
> Greetings
>
> The following code prints FFFF0000. Similar thing happens when 
> a and b are
> bytes.
> Is this intended?
>
> Regards
> - Puneet
>
> void main()
> {
>   import std.stdio;
>   ushort a = 0x55AA;
>   ushort b = 0xAA55;
>   writefln("%X", ~(a | b));
> }

integer operations are always promoted to at least int. That's 
standard fare since C. I think it is a performance thing: Unpack 
into ints, oeprate, repack into ints.

D's stance regarding integer operations is "if it compiles, it 
creates the same output as in C".

It's kind of a gotcha, but not that big a deal.


More information about the Digitalmars-d mailing list