472,982 Members | 2,439 Online

What does this code do?

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
Feb 6 '08 #1
9 1944
James Dow Allen wrote:
>
.... snip ...
>
Fragment #1
int hw(uint64 x) {
uint64 y;

y = x & (x >H0);
nothing. H0 is undefined.

.... snip ...
>
Fragment #2
void baz(int b, int c) {
int a, d, e, f, g;

if (e = b, g = a = -1, c)
while (d = b) {
nothing. d is uninitialized.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Feb 6 '08 #2
On Wed, 06 Feb 2008 00:20:17 -0500, CBFalconer wrote:
James Dow Allen wrote:
>>
(...)
>>
Fragment #2
void baz(int b, int c) {
int a, d, e, f, g;

if (e = b, g = a = -1, c)
while (d = b) {

nothing. d is uninitialized.
'd' is initialised using input argument 'b' (it is not a comparison to 'b').

- Anand

--
Feb 6 '08 #3
CBFalconer said:
James Dow Allen wrote:
>>
... snip ...
>>
Fragment #1
int hw(uint64 x) {
uint64 y;

y = x & (x >H0);

nothing. H0 is undefined.
Chuck, don't you think James *knows* he hasn't defined H0? He's not exactly
J Random Newbie.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Feb 6 '08 #4
Anand Hariharan wrote:
CBFalconer wrote:
>James Dow Allen wrote:
>>>
(...)
>>>
Fragment #2
void baz(int b, int c) {
int a, d, e, f, g;

if (e = b, g = a = -1, c)
while (d = b) {

nothing. d is uninitialized.

'd' is initialised using input argument 'b' (it is not a
comparison to 'b').
Woops - you're right.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Feb 6 '08 #5
In article <Ab******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>CBFalconer said:
>James Dow Allen wrote:
>>>
... snip ...
>>>
Fragment #1
int hw(uint64 x) {
uint64 y;

y = x & (x >H0);

nothing. H0 is undefined.

Chuck, don't you think James *knows* he hasn't defined H0? He's not exactly
J Random Newbie.
Oh. The. Irony.

Note that recently the regs are getting more and more open (and openness
is a good thing - don't get me wrong!) about the fact that there are
different rules for the Clique than for, as RH so quaintly puts it, J
Random Newbie.

Note also that CBF's Clique membership seems to be in a probationary
status these days...

Feb 6 '08 #6
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <Ab******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>CBFalconer said:
>>James Dow Allen wrote:

... snip ...

Fragment #1
int hw(uint64 x) {
uint64 y;

y = x & (x >H0);

nothing. H0 is undefined.

Chuck, don't you think James *knows* he hasn't defined H0? He's not exactly
J Random Newbie.

Oh. The. Irony.

Note that recently the regs are getting more and more open (and openness
is a good thing - don't get me wrong!) about the fact that there are
different rules for the Clique than for, as RH so quaintly puts it, J
Random Newbie.

Note also that CBF's Clique membership seems to be in a probationary
status these days...
"Chuck" is probably one of the most useless posters to a Usenet
technical group I have ever witnessed. No wonder his code is so awful -
I doubt if he was ever allowed to post in a real team as I have rarely
seen anyone so antisocial and up themselves. Better to killfile him,
much as a I abhor doing so.

Feb 6 '08 #7
Re: What does this code do?

Decrease productivity of newsgroup readers ? :-)
Feb 7 '08 #8
On Feb 9, 5:51 am, Dave Hansen <i...@hotmail.comwrote:
On Feb 8, 3:12 am, James Dow Allen <jdallen2...@yahoo.comwrote:
[...]
Richard Bos wrote:
that I can't be bothered to figure out.
He didn't say he couldn't, but that he couldn't be bothered.
Apparently he's decided it's beneath him.

But I like a puzzle. I'm thinking "spiral..."
Yes, with a nearby spiralling thread I'm surprised
only Dave got this. It would be interesting to
know why Bos disliked the code so much. (Omission
of comments was deliberate.) I don't generally pack
multiple statements onto one line, but did so
almost whimsically in the fragment. Avoiding
new lines seems not too egregious in simple idioms like
d = e, e = a, a = d; /* swap */

All other things being equal (though of course
they never are!) the succincter code is the better
code: one FOR is better than four FOR's. For this
reason, my spiraller seems OK.

James
Feb 11 '08 #9
In article <47*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
....
>Get off your friggin' high horse, mister perfect-maths-wizard. You have
not the slightest idea how I tested your program wand why I could not be
arsed to do more of an effort. But since you asked for it, I'll tell you
now: I couldn't be arsed because it was crappy code, used in a crappy
puzzle, by a crappy programmer who should have known better than to
consider that crap even remotely acceptable. And in the future, I won't
even be making what little effort I have this time; your programming