When the %x, %o or %b format is used to format a negative integer, the correct result is produced only in the case of long. Even for byte, short or int, the output is 64 bits long.
----------
import std.stdio;
const char[][] fmts = [ "%x", "%o", "%b" ];
void main() {
foreach (fmt; fmts) {
byte b = byte.max;
writefln(fmt, b);
writefln(fmt, ++b);
writefln(fmt, ++b);
short s = short.max;
writefln(fmt, s);
writefln(fmt, ++s);
writefln(fmt, ++s);
int i = int.max;
writefln(fmt, i);
writefln(fmt, ++i);
writefln(fmt, ++i);
long l = long.max;
writefln(fmt, l);
writefln(fmt, ++l);
writefln(fmt, ++l);
writefln();
}
}
----------
The %x case should suffice to illustrate.
Actual output:
7f
ffffffffffffff80
ffffffffffffff81
7fff
ffffffffffff8000
ffffffffffff8001
7fffffff
ffffffff80000000
ffffffff80000001
7fffffffffffffff
8000000000000000
8000000000000001
Expected output:
7f
80
81
7fff
8000
8001
7fffffff
80000000
80000001
7fffffffffffffff
8000000000000000
8000000000000001
The same happens if I specify e.g. %04x instead of %x.