This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.
From a random post about unsigned integers, a trick I've never seen for looping downwards. With unsigned ints this never terminates because i underflows: for (i = len - 1; i >= 0; i--) But with wrapping math, this does the right thing, even when len == 0! for (i = len - 1; i < len; i--) It is neat that it is the same loop condition as when iterating forwards, too. (I'm not sure I'd ever write such…
From a random post about unsigned integers, a trick I've never seen for looping downwards.
With unsigned ints this never terminates because i underflows:
for (i = len - 1; i >= 0; i--)
But with wrapping math, this does the right thing, even when len == 0!
for (i = len - 1; i < len; i--)
It is neat that it is the same loop condition as when iterating forwards, too.
(I'm not sure I'd ever write such code, but it's still neat.)
Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.