Hacker News new | past | comments | ask | show | jobs | submit login

The unary complement operator should get you there:

  unsigned int x = 0;
  const unsigned int max = ~x;

  while (x <= max) {
     calc(x++);
  }
Or,

  #include <limits.h>

  const unsigned int max = UINT_MAX;



This is equivalent to

  while (1) {
    calc(x++);
  }
which is an infinite loop, since the expression x <= max is always true


Heh! Good catch! That is what I get for not testing. After UINT_MAX, x wraps around to 0.


Or if you only wanted to iterate halfway:

    unsigned int x = 0;

    while (x < ~x) {
        calc(x++);
    }
Or going down:

    unsigned int x = ~0;

    while (x > ~x) {
        calc(x--);
    }


By "halfway", did you mean "off by one"?


no




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: