J
James Dow Allen
How about this idea? Post fragments of C code which
seem fun, interesting or instructive. Puzzles can
be posed in various ways. (What does this do? Can you
see the bug? How to code this for better efficiency?
Code for better readability, etc.) Best, perhaps, would
be fragments from another's admirable application.
"What does this do?" puzzles might need comments removed
and symbols obfuscated, as I've done in both of the
following fragments.
Fragments #1 and #2 are unrelated to each other.
Please "reverse-engineer" them before Googl'ing for
exact provenance.
Fragment #1
int hw(uint64 x)
{
uint64 y;
y = x & (x >> H0);
if (y & (y >> 2 * H0))
return 1;
y = x & (x >> H1);
if (y & (y >> 2 * H1))
return 1;
y = x & (x >> H2);
if (y & (y >> 2 * H2))
return 1;
y = x & (x >> 1);
return (y & (y >> 2));
}
Fragment #2
void baz(int b, int c)
{
int a, d, e, f, g;
if (e = b, g = a = -1, c)
while (d = b) {
b = c-1, f = c = d;
while (f--)
foo(g -= a);
d = -e, e = a, a = d;
}
}
What does each of these fragments do?
James Dow Allen
seem fun, interesting or instructive. Puzzles can
be posed in various ways. (What does this do? Can you
see the bug? How to code this for better efficiency?
Code for better readability, etc.) Best, perhaps, would
be fragments from another's admirable application.
"What does this do?" puzzles might need comments removed
and symbols obfuscated, as I've done in both of the
following fragments.
Fragments #1 and #2 are unrelated to each other.
Please "reverse-engineer" them before Googl'ing for
exact provenance.
Fragment #1
int hw(uint64 x)
{
uint64 y;
y = x & (x >> H0);
if (y & (y >> 2 * H0))
return 1;
y = x & (x >> H1);
if (y & (y >> 2 * H1))
return 1;
y = x & (x >> H2);
if (y & (y >> 2 * H2))
return 1;
y = x & (x >> 1);
return (y & (y >> 2));
}
Fragment #2
void baz(int b, int c)
{
int a, d, e, f, g;
if (e = b, g = a = -1, c)
while (d = b) {
b = c-1, f = c = d;
while (f--)
foo(g -= a);
d = -e, e = a, a = d;
}
}
What does each of these fragments do?
James Dow Allen