#include #include // Prints the binary representation of the input as a string. template void printf_binary(T n) { T mask = 1 << sizeof(T) * 8 - 1; fprintf(stderr, (mask & n) ? "1" : "0"); mask = 1 << sizeof(T) * 8 - 2; for (; mask != 0; mask >>= 1) { fprintf(stderr, (mask & n) ? "1" : "0"); } } // Scans a T in binary format. The input is expected to contain at // most sizeof(T) * 8 consecutive bits. template T sscanf_binary(char *p) { while (isspace(*p)) { p++; } T n = 0; for (;;) { if (*p == '1') { n |= 1; } else if (*p != '0') { break; } n <<= 1; } return n; } template T binary_next(T n) { T t = n & (-n); T r = n + t; return ((r ^ n) / (4 * t)) | r; } typedef char inttype; int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage %s: ", argv[0]); return 0; } int i; if (sscanf(argv[1], "%d", &i) != 1 || i == 0 || i > sizeof(char) * 8 - 1) { printf("Parameter is not a positive integer or too large!"); return 0; } inttype n = (2 << (i - 1)) - 1; inttype e = n << sizeof(inttype) * 8 - (i + 1); printf_binary(n); fprintf(stderr, "\n"); while (n != e) { n = binary_next(n); printf_binary(n); fprintf(stderr, "\n"); } return 0; }