Velocity Reviews > What does this code do?

# What does this code do?

James Dow Allen
Guest
Posts: n/a

 02-06-2008

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

CBFalconer
Guest
Posts: n/a

 02-06-2008
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

Anand Hariharan
Guest
Posts: n/a

 02-06-2008
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

--

Richard Heathfield
Guest
Posts: n/a

 02-06-2008
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

Richard Bos
Guest
Posts: n/a

 02-06-2008
James Dow Allen <(E-Mail Removed)> wrote:

> Fragment #1
> int hw(uint64 x)
> {
> uint64 y;
>
> y = x & (x >> H0);
> if (y & (y >> 2 * H0))
> return 1;

Checks whether certain groups of 4 bits are all set. Which 4 bits
exactly depends on the values of H0/1/2, but the lowest 4 bits are
always involved because of the literal 1 and 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;
> }
> }

Outputs all numbers N from 0 <= N < b*c, in some shuffled order that I
can't be bothered to figure out.

> What does each of these fragments do?

I'll tell you what both of them would do if I encountered them in real
code, though. They'd make me tell off their coders. There may be some
justification for the first, if H0/1/2 are given proper names and some
comments are added. There can be no justification for the latter,
outside the IOCCC.

Richard

CBFalconer
Guest
Posts: n/a

 02-06-2008
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

Kenny McCormack
Guest
Posts: n/a

 02-06-2008
In article <(E-Mail Removed)>,
Richard Heathfield <(E-Mail Removed)> wrote:
>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...

Richard
Guest
Posts: n/a

 02-06-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (Kenny McCormack) writes:

> In article <(E-Mail Removed)>,
> Richard Heathfield <(E-Mail Removed)> wrote:
>>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.

christophe.4.news@gmail.com
Guest
Posts: n/a

 02-06-2008
Re: What does this code do?

Decrease productivity of newsgroup readers ?

CBFalconer
Guest
Posts: n/a

 02-07-2008
Richard Heathfield wrote:
> 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.

I rarely pay any great attention to the writers identity. It's
still undefined.

--
[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