[Issue 1535] New: incorrect casting of 8 bit quantities to longer precision

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Sep 27 18:28:14 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1535

           Summary: incorrect casting of 8 bit quantities to longer
                    precision
           Product: D
           Version: 1.021
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: grahamc001uk at yahoo.co.uk


Casting 8 bit quantities to longer precision does wrong sign extension.
bytes get sign extended and chars do not.
It should be the other way around.

Test program:


// Compiled with Digital Mars D Compiler v1.021
// on Windows XP.
import std.stdio;

void main() {
        char    c8; short c16; int c32;
        byte    b8; ushort b16; uint b32;

        c8 = 0x80;
        b8 = cast(byte) 0x80;

        c16 = cast(short) c8;
        b16 = cast(ushort) b8;

        c32 = cast(int) c8;
        b32 = cast(uint) b8;

        // These asserts are present to prove this is not a feature of
writefln().
        assert(cast(ushort)b8 == 0xFF80);
        assert(cast(uint)b8 == 0xFFFFFF80);

        writefln("c %X", c8);
        writefln("b %X", b8);

        // chars should be sign extended and bytes not sign extended.
        // but the reverse actually happens.

        writefln("c %X", c16);
        writefln("b %X", b16);

        writefln("c %X", c32);
        writefln("b %X", b32);

        // This program generates the following output:
        // c 80
        // b 80
        // c 80
        // b FF80
        // c 80
        // b FFFFFF80

}


-- 



More information about the Digitalmars-d-bugs mailing list