Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What does this code do? (http://www.velocityreviews.com/forums/t589799-what-does-this-code-do.html)

James Dow Allen 02-06-2008 04:58 AM

What does this code do?
 

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 02-06-2008 05:20 AM

Re: What does this code do?
 
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>
Try the download section.


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


Anand Hariharan 02-06-2008 05:35 AM

Re: What does this code do?
 
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

--
ROT-13 email address to reply.

Richard Heathfield 02-06-2008 08:14 AM

Re: What does this code do?
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Richard Bos 02-06-2008 11:37 AM

Re: What does this code do?
 
James Dow Allen <jdallen2000@yahoo.com> 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 02-06-2008 01:16 PM

Re: What does this code do?
 
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>
Try the download section.



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


Kenny McCormack 02-06-2008 04:10 PM

Re: What does this code do?
 
In article <AbadnfltGfwU9jTanZ2dnUVZ8umdnZ2d@bt.com>,
Richard Heathfield <rjh@see.sig.invalid> 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 02-06-2008 09:24 PM

Re: What does this code do?
 
gazelle@xmission.xmission.com (Kenny McCormack) writes:

> In article <AbadnfltGfwU9jTanZ2dnUVZ8umdnZ2d@bt.com>,
> Richard Heathfield <rjh@see.sig.invalid> 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 02-06-2008 10:50 PM

Re: What does this code do?
 
Re: What does this code do?

Decrease productivity of newsgroup readers ? :-)

CBFalconer 02-07-2008 06:22 AM

Re: What does this code do?
 
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>
Try the download section.



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



All times are GMT. The time now is 11:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.