Funny old trick

Discussion in 'C Programming' started by BGreene, Jul 6, 2005.

  1. BGreene

    BGreene Guest

    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.
     
    BGreene, Jul 6, 2005
    #1
    1. Advertising

  2. 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.
     
    Martin Ambuhl, Jul 6, 2005
    #2
    1. Advertising

  3. BGreene

    BGreene Guest

    "Martin Ambuhl" <> wrote in message
    news:zpKye.5172$...
    > 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. :)
     
    BGreene, Jul 6, 2005
    #3
  4. BGreene

    Guest

    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?
     
    , Jul 6, 2005
    #4
  5. 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).
     
    Martin Ambuhl, Jul 6, 2005
    #5
  6. BGreene

    Guest

    Martin Ambuhl ha escrito:
    > 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
     
    , Jul 6, 2005
    #6
  7. 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.
     
    Martin Ambuhl, Jul 6, 2005
    #7
  8. BGreene

    Guest

    Martin Ambuhl ha escrito:
    > 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
     
    , Jul 6, 2005
    #8
  9. BGreene

    Alan Balmer Guest

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

    > 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
     
    Alan Balmer, Jul 6, 2005
    #9
  10. In article <> writes:
    > On Wed, 06 Jul 2005 15:37:02 GMT, Martin Ambuhl
    > <> 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/
     
    Dik T. Winter, Jul 7, 2005
    #10
  11. BGreene

    CBFalconer Guest

    "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
     
    CBFalconer, Jul 7, 2005
    #11
  12. In article <> 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/
     
    Dik T. Winter, Jul 7, 2005
    #12
  13. BGreene

    CBFalconer Guest

    "Dik T. Winter" wrote:
    > 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
     
    CBFalconer, Jul 7, 2005
    #13
  14. In article <> writes:
    > "Dik T. Winter" wrote:
    > > 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/
     
    Dik T. Winter, Jul 8, 2005
    #14
  15. 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?
     
    Michel Rouzic, Jul 8, 2005
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Tuttle
    Replies:
    2
    Views:
    457
    Don Tuttle
    Oct 13, 2003
  2. Jim Hill
    Replies:
    3
    Views:
    405
    Jim Hill
    Feb 12, 2007
  3. *Prot3anThr3ad*

    old repository for old C++ source code

    *Prot3anThr3ad*, Sep 29, 2006, in forum: C++
    Replies:
    6
    Views:
    382
    *Prot3anThr3ad*
    Oct 2, 2006
  4. John Henry
    Replies:
    24
    Views:
    1,018
    alex23
    May 30, 2008
  5. George Mpouras

    An old trick that save me TONES of code

    George Mpouras, May 28, 2013, in forum: Perl Misc
    Replies:
    5
    Views:
    194
    Rainer Weikusat
    May 29, 2013
Loading...

Share This Page