Funny old trick

B

BGreene

I was not going to post this but my someone thought it was funny,

Write the shortest c statement that will "exchange" two unsigned ints. in
place.
 
M

Martin Ambuhl

BGreene said:
I was not going to post this but my someone thought it was funny,

Write the shortest c statement that will "exchange" two unsigned ints. in
place.

Tired. Trite. Old hat. Dead horse.
 
M

Martin Ambuhl

Didn't get it. Could you please explain?

This is a beginner's trick that I first was exposed to in 1965. Every
year since there has been a new cohort that thought it cute. It's not;
it's stupid.

What you didn't get is the FAQ.
from <http://www.eskimo.com/~scs/C-faq/q10.3.html> we have
(and the ``obvious'' supercompressed implementation for integral
types a^=b^=a^=b is illegal due to multiple side-effects; see
question 3.2).
 
E

ehames

Martin Ambuhl ha escrito:
This is a beginner's trick that I first was exposed to in 1965. Every
year since there has been a new cohort that thought it cute. It's not;
it's stupid.

What you didn't get is the FAQ.
from <http://www.eskimo.com/~scs/C-faq/q10.3.html> we have
(and the ``obvious'' supercompressed implementation for integral
types a^=b^=a^=b is illegal due to multiple side-effects; see
question 3.2).

Oh! I have read that before. What I didn't get was:

Sorry for not being more specific.

Cheers,
Ed
 
M

Martin Ambuhl

Oh! I have read that before. What I didn't get was:
Sorry for not being more specific.

Are you purposely being obscure? What part of characterizing a tired,
trite, and old hat hack as being tired, trite, and old hat. Do you
enjoy beating dead horses? Please, explain what part of a completely
clear characterization you do not understand.
 
E

ehames

Martin Ambuhl ha escrito:
Are you purposely being obscure? What part of characterizing a tired,
trite, and old hat hack as being tired, trite, and old hat. Do you
enjoy beating dead horses? Please, explain what part of a completely
clear characterization you do not understand.

Absolutely not. I didn't understand your characterization of the hack
as such, maybe due to the fact that English is not my native language.
I didn't intend to be obscure or annoying.

Thanks for your response,
Ed
 
A

Alan Balmer

This is a beginner's trick that I first was exposed to in 1965. Every
year since there has been a new cohort that thought it cute. It's not;
it's stupid.
When I discovered it independently, (in assembler, not C) in the
course of writing a emulated byte addressing capability for the 16-bit
Varian computers, I thought it was quite clever of me.
 
D

Dik T. Winter

> On Wed, 06 Jul 2005 15:37:02 GMT, Martin Ambuhl

> When I discovered it independently, (in assembler, not C) in the
> course of writing a emulated byte addressing capability for the 16-bit
> Varian computers, I thought it was quite clever of me.

Oh, it is quite clever. But there are so many people that discovered
it independently. The whole point is that in C it makes no sense.
Now, how about getting the decimals of an unsigned integer number below
1000 using only shifts and additions/subtractions...
 
C

CBFalconer

Dik T. Winter said:
O, I do it in 15 instructions, 9 shifts and 6 adds/subtracts, and
nothing conditional in between.

Possibly of use in some heavily resource limited context, but I see
no real purpose for that size of value. I resurrected and
published that technique to handle bin-dec conversion of bignums,
where long drawn out divisions and modulos can be somewhat time and
memory consuming.
 
D

Dik T. Winter

>
> Possibly of use in some heavily resource limited context, but I see
> no real purpose for that size of value.

Indeed. Think CDC Cyber where the generation number of a permanent file
was at most 999. My comment was tongue-in-cheek. If you are not
resource limited, use the clearest way to express what you want.
> I resurrected and
> published that technique to handle bin-dec conversion of bignums,
> where long drawn out divisions and modulos can be somewhat time and
> memory consuming.

Yes, and in those circumstances shifts are also somewhat time and
memory consuming. I have written (back in the eighties) some of such
routines for a primality proving package (that should work for numbers
up to about 300 decimal digits). Reading in and printing out such
numbers is about the most insignificant part of the program, and I
did it quite naturally. If you are using bignums it makes no sense
to spend time to print a lot of the numbers you generate. Nobody will
ever read your output.
 
M

Michel Rouzic

BGreene said:
I was not going to post this but my someone thought it was funny,

Write the shortest c statement that will "exchange" two unsigned ints. in
place.

ok, not sure I got it right... the point is to exchange two values
without using an intermediate variable? i'm not sure to get the point,
but this should do the trick... (provided that a+b isn't greater than
the greatest possible value)

a+=b;
b=a-b;
a=a-b;

...right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top