Bug 1535 – incorrect casting of 8 bit quantities to longer precision

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-09-27T20:28:00Z
Last change time
2014-02-17T22:50:16Z
Assigned to
bugzilla
Creator
grahamc001uk

Comments

Comment #0 by grahamc001uk — 2007-09-27T20:28:13Z
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 }
Comment #1 by shro8822 — 2007-09-27T20:43:26Z
I think this bug is invalid: byte should get sign extended as it is a signed value (ubyte is unsigned). char is not technically a number so I'm not sure what should happen when it is cast to an int. (or short, or long, etc.) When it is cast to a large char (wchar or dchar) it should be converted to the equivalent value there (which bitwise is IIRC the same as a unsigned cast from a ubyte to ushort or uint) based on that the cast is correct in both cases. I;m marking this as invalid because one cases is invalid and the other probably is.
Comment #2 by braddr — 2007-09-27T21:03:22Z
See also: http://www.digitalmars.com/d/type.html char is unsigned, so its behavior is also correct.
Comment #3 by bugzilla — 2007-09-28T03:19:25Z
This works as designed. bytes are signed, ubytes are unsigned. chars are unsigned because they have valid values >=0x80, which would screw up if they were sign extended.
Comment #4 by grahamc001uk — 2007-09-28T05:03:56Z
Sorry, that was my mistake. You might want to consider a slight change to the overview page http://www.digitalmars.com/d/overview.html line: "No more uncertainty about the signed-ness of chars" as that can be mis-read as implying they are signed (like on many C compilers). --- [email protected] wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=1535 > > > > > > ------- Comment #3 from [email protected] > 2007-09-28 03:19 ------- > This works as designed. bytes are signed, ubytes are > unsigned. chars are > unsigned because they have valid values >=0x80, > which would screw up if they > were sign extended. > > > -- > Configure bugmail: > http://d.puremagic.com/issues/userprefs.cgi?tab=email > ------- You are receiving this mail because: ------- > You reported the bug, or are watching the reporter. > --- --- ___________________________________________________________ Yahoo! Answers - Got a question? Someone out there knows the answer. Try it now. http://uk.answers.yahoo.com/