Problem with object understanding and datatypes

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Fri May 24 19:26:03 PDT 2013


On Saturday, 25 May 2013 at 01:03:53 UTC, Namal wrote:
>
>> 255 - 129 is less than 128 so the result is T.max, which is 
>> 255, which is not equal to 0.
>
>
> I dont understand this at all 255 - 129 should be 126 in ubyte 
> or not?

I checked, and operation between two ubyte is an int. When you 
cast that int to ubyte, it gets its least significant byte 
represented as ubyte.

import std.stdio;

void main() {
     ubyte x = 128;
     ubyte y = 129;
     writeln(cast(ubyte)(x - y)); //prints 255
     writeln(x - y); //prints -1
     writeln(typeof(x - y).stringof); //prints 'int' !!!!
}

Also, I tried the code you pasted, and the reason it fails the 
asserts is that there's something wrong in the if conditions in 
opBinary (and also, that 'rhs.max - rhs._value' didn't compile).

The following makes your asserts pass:

     ...
     static if (op == "-") {
         if(_value < rhs._value)
             return rhs.min;
     }
     static if (op == "+") {
         if(_value > max._value - rhs._value)
             return rhs.max;
     }
     ...


--jm



More information about the Digitalmars-d-learn mailing list