I found this in a piece of code I am trying to understand. It is probably some clever trick, but I don't get it. When is
unsigned(x-dx)<unsigned(size)
true? x, dx, and size are all (signed) int. From the context I guess, that x-dx should not be smaller than zero and not be larger than size. But I don't understand the unsigned trick.
Could someone explain it or tell where I can find more info about it?
thx in advance
This code determines if x - dx
is in the range [0
... size
): if x - dx
is negative, it becomes a large positive value when cast to unsigned
. Large in this context means, a value larger than any positive value of the type [signed
] int
. That is, the comparison
unsigned(x - dx) < unsigned(size)
is true if x - dx
is non-negative and smaller than size
.