Question about ubyte x overflow, any safe way?

Max Haughton maxhaton at gmail.com
Sun Aug 4 18:15:30 UTC 2019


On Sunday, 4 August 2019 at 18:12:48 UTC, matheus wrote:
> Hi,
>
> The snippet below will produce an "infinite loop" because 
> obviously "ubyte u" will overflow after 255:
>
> import std.stdio;
> void main(){
>     ubyte u = 250;
>     for(;u<256;++u){
>         writeln(u);
>     }
> }
>
> Question: Is there a way (Flag) to prevent this?
>
> Matheus.

What do you want to do? If you just want to count to 255 then use 
a foreach


If you want to prevent overflow you must either use BigInt or 
wrap ubyte in a struct that doesn't allow overflow


More information about the Digitalmars-d-learn mailing list