Swap Integers

This is a discussion on Swap Integers within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Dave_Sinkula [edit]And frankly, fixing a problem that does not exist is in itself a problem. Wait. So ...

  1. #46
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Dave_Sinkula View Post
    [edit]And frankly, fixing a problem that does not exist is in itself a problem.
    Wait. So we're saying that the XOR trick is a problem, or not a problem? I'm confused.

  2. #47
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by laserlight View Post
    Yes, since the C++ Standard leaves the exact result of a right shift as implementation defined for negative integers. For non-negative integers it is quite clear that a division by the nth power of two will be performed. So, (1 >> 1) == 0, but (-1 >> 1) == 0 or (-1 >> 1) == -1 are both possible, as are other interpretations of what happens on a right shift (e.g., largest possible positive integer value).
    Out of instinct, I would never shift right to divide when I knew that the value might be negative. It's sometimes hard for me to gauge what should be "obvious." Again, I mentioned it simply for the sake of mentioning it.

  3. #48
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My impression is that it was an '70's problem with the price of memory affecting hardware manufacturer's choices: namely, have less memory. So due to the lack of available registers in hardware, the XOR swap was developed by clever programmers. As soon as the unavailability of hardware registers disappeared, so did the problem that this addressed. So computer hardware developed since at least the early '80's already solves the problem this solution seeks to solve. Thus making it a non-issue.

    Unless programmers are still taught that we live in the 1970's. Then this can screw up some compilers that were raised to understand that the programmers writing the code already knew that.

    So apparently now compilers are supposed to solve problems that programmers are putting in practice that solve problems that have already been solved elsewise, but the programmer has yet to understand this.

    Why shouldn't programming be a little simpler?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #49
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Okay okay, I give up... In the future, I'll keep stuff like this a secret. When newbie folks see it in code they'll just be stuck wondering "What the hell is THAT?"

    EDIT: Also, if I really used this stuff in my own code, don't you think I would have known about the swap-with-self problem off the top of my head? So obviously I don't do this.

  5. #50
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Again, you seem to misunderstand the thwarting I've undergone.

    Sure it helps to know things about the underlying implementation, but that is not completely or entirely relevant.

    Times change.

    Just as sure as I can say, "Integer math is faster than floating point math," such things are subject to the times. Surely someone will discover this thread in a decade and try to ask me what I was smoking.

    [My answer: a legal tobacco cigarette in the United States in my own garage. This itself may have changed in a decade.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #51
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Dave_Sinkula View Post
    Just as sure as I can say, "Integer math is faster than floating point math," such things are subject to the times. Surely someone will discover this thread in a decade and try to ask me what I was smoking.
    Funny you choose that example. I think that ceased to be true a few years ago, at least on the systems I was testing on. It seems like casting between the two is the source of slowness these days. Get your data into doubles and keep it there!

    I make assumptions based on obsolete experience, too. I try to challenge my own assumptions as often as possible, but sometimes there just aren't enough useful hours in the day...

  7. #52
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    > The XOR swap thing merely happens to be among them, but not (yet) in the FAQ.
    Are we inany hurry at all to change this? Just curious.

  8. #53
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    for simple stuff, try to use refs more, they provide cleaner syntax than pointers
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  9. #54
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The XOR swap thing merely happens to be among them, but not (yet) in the FAQ.
    I believe it's in the C faq.

  10. #55
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    But I agree with brewbuck, it's a good trick to know, the reason being some expert will come along and ridicule you for not knowing it. Just like if you don't know a certain acronym, you will get the same reaction.

  11. #56
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Ridicule the "expert" in return for holding on to archaic, useless trivia.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #57
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by CornedBee View Post
    Ridicule the "expert" in return for holding on to archaic, useless trivia.
    Some of us can't help but hold on to pretty much everything interesting we ever saw. At times this is a real curse, because it makes me look like a loser who wasted his time memorizing useless facts. Hey, they just go in there and stay there, there's nothing conscious about it

  13. #58
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh, I know that. It's very much imprinted in my brain, too. But I try to avoid spreading the knowledge about these things.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #59
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by CornedBee View Post
    Oh, I know that. It's very much imprinted in my brain, too. But I try to avoid spreading the knowledge about these things.
    I shall endeavor to be more esoteric.

  15. #60
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose the ratio of useful / useless knowledge I have is 0 - at least in integer math.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21