473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Funny old trick

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.

Nov 15 '05 #1
14 1969
BGreene wrote:
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.
Nov 15 '05 #2

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:zp*****************@newsread1.news.atl.earthl ink.net...
BGreene wrote:
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.

Exactly what i told her. :-)
Nov 15 '05 #3
Martin Ambuhl wrote:
BGreene wrote:
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.


Didn't get it. Could you please explain?

Nov 15 '05 #4
eh****@gmail.com wrote:
Martin Ambuhl wrote:
BGreene wrote:
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.

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).
Nov 15 '05 #5
Martin Ambuhl ha escrito:
eh****@gmail.com wrote:
Martin Ambuhl wrote:
BGreene wrote:

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.


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).


Oh! I have read that before. What I didn't get was:
Tired. Trite. Old hat. Dead horse.


Sorry for not being more specific.

Cheers,
Ed

Nov 15 '05 #6
eh****@gmail.com wrote:
Oh! I have read that before. What I didn't get was:
Tired. Trite. Old hat. Dead horse.
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.
Nov 15 '05 #7
Martin Ambuhl ha escrito:
eh****@gmail.com wrote:
Oh! I have read that before. What I didn't get was:
>Tired. Trite. Old hat. Dead horse.

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.


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

Nov 15 '05 #8
On Wed, 06 Jul 2005 15:37:02 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote:
eh****@gmail.com wrote:
Martin Ambuhl wrote:
BGreene wrote:

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.

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.

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.
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).


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #9
In article <4t********************************@4ax.com> al******@spamcop.net writes:
On Wed, 06 Jul 2005 15:37:02 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote:

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


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...
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #10
"Dik T. Winter" wrote:
.... snip ...
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...


Why limit yourself to values below 1000? See:

<http://cbfalconer.home.att.net/download/dubldabl.txt>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #11
In article <42***************@yahoo.com> cb********@worldnet.att.net writes:
"Dik T. Winter" wrote:

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


Why limit yourself to values below 1000? See:

<http://cbfalconer.home.att.net/download/dubldabl.txt>


O, I do it in 15 instructions, 9 shifts and 6 adds/subtracts, and
nothing conditional in between.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #12
"Dik T. Winter" wrote:
cb********@worldnet.att.net writes:
"Dik T. Winter" wrote:

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


Why limit yourself to values below 1000? See:

<http://cbfalconer.home.att.net/download/dubldabl.txt>


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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #13
In article <42***************@yahoo.com> cb********@worldnet.att.net writes:
"Dik T. Winter" wrote:
cb********@worldnet.att.net writes: ....
Why limit yourself to values below 1000? See:

<http://cbfalconer.home.att.net/download/dubldabl.txt>
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.


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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #14


BGreene wrote:
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?

Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
4
by: Will Stuyvesant | last post by:
Add your funny or surprising Python error messages to this thread. A requirement is that you should also show (minimal) code that produces the message. Put the code below, so people can think...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
4
by: jonas.email | last post by:
Im am doing a major project and after a long time debugging i found this rather funny (=annoying) bug. In its simple form, this is the main problem: int main(void) { float f1 = 0.3f; float f2...
8
by: MarsFossils | last post by:
I am giving a lunch and learn talk on how to "Create your own Web Page" tomorrow and would like to mention a funny anecdote with an inspiring moral about web page design. Does anybody have any...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
11
by: Paul | last post by:
Hello Everyone, I hope I'm posting this in the right forum. I have a problem with a VB.net application where out of thousands of users, only the people at two specific companies experience extra...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.