unsigned policy

orgoton orgoton at mindless.com
Wed Feb 7 12:31:57 PST 2007


I always use unsigned variables when their values don't go below 0. I also always use the smallest variable possible.

More often than not, I use "ubyte" instead of "int" in "for" loops. Signed variables use the most significant bit to represent sign. I don't know if there's any performance gain (even if marginal) when using mathematical operations on unsigned variables.

About conditions, you can always force a variable to be unsigned by masking away it's most significant bit:

short var2;
(...)
unsigned=var2 && 0x3FFF; (0x3FFF is hexadecimal for 0111_1111_1111_1111)

but it would be simpler just to use "abs()" function to obtain the absolute value.



More information about the Digitalmars-d mailing list