Skip to main content
GameDev.net gamedev.net
🔒 Locked

A programming riddle :)

Started by Muncher Apr 26, 2004 at 11:52 PM 32 replies 2.3k views
Original Post
Muncher
Muncher
Hello!! I have a riddle for you guys See if you can solve it. i have two variables, a and b. they can be any value of type integer. I want the swap the contents of a and b, without using a 3rd variable. ie/ a = 10; b = 5; .... .... a = 5; b = 10;
fractoid
fractoid
Yep, we know the answer.
Kentaro
Kentaro
Yep, I''ve got it.
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
Sneftel
Sneftel
quote:
Original post by Anonymous Poster
This actually can''t be done in the general case. What if a == b?

Then you just return.


"Sneftel is correct, if rather vulgar." --Flarelocke
caseyd
caseyd
Here''s two ways of doing it:

int a = 10;
int b = 5;

a ^= b;
b ^= a;
a ^= b;

Or method two:

int a = 10;
int b = 5;

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

Muncher
Muncher
YAY -

btw It wasn''t homework, i was asked the question in an aptitude test for a programming position and it stumped me for a bit

Muncher
Muncher
what is the ^= operator... never used that b4
Mastaba
Mastaba
^ is the XOR operator.
neoztar
neoztar
Both will work in any case.


// add method using chars ( easier to see with small numbers )

a = 255
b = 255

a += b // a = 510, overflows to 254
b = a - b; // b = -1, overflows to 255
a -= b // a = -1, overflows to 255

// xor method

a = 255
b = 255

a ^= b // a = 0
b ^= a // b = 255
a ^= b // a = 255


~nz

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
izzo
izzo
I got that in an interview once. Stumped me too.. the interviewer ended up telling me "think XOR" and on the way home in the train I figured it out on paper :-)

sam
haro
haro
quote:
Original post by Muncher
YAY -

btw It wasn''t homework, i was asked the question in an aptitude test for a programming position and it stumped me for a bit




Might I ask which company? Knowing these little tricks is cute and all but they are no longer really applicable. Xor swapping does little more than obfuscate code and prevent compiler side optimizations. This for the ''benefit'' of possibly gaining a couple of cycles. It also seems a very inappropriate question to ask since it seems more oriented towards memorization as opposed to logical problem solving.
WonderWorld
WonderWorld
It''s really a very strange question. It''s more interesting to know if a guy fits in the team. If he has good programming and problem solving skills and is decicated.
izzo
izzo
Well it''s a question of problem solving, really. I think the idea is if you haven''t heard the problem before and you know certain basic things then you should be able to "think outside the box" and come up with a solution.

Of course, you can probably quite easily find lists of similar questions on the internet and just memorise the answers.

A better task might be to ask the interviewee to write some code that implemented something non-trivial.

cheers
sam
haro
haro
quote:
Original post by izzo
Well it''s a question of problem solving, really. I think the idea is if you haven''t heard the problem before and you know certain basic things then you should be able to "think outside the box" and come up with a solution.


I really disagree here. There has to be a point where a problem drifts from the problem solving realm and into the memorization realm. This problem is entering the memorization realm if not already fully in it. XORing is not a typical operation for programmers. Encryption and animation are the only two applications I know of that have any potential use for XORing in. It also has no relevant analog with typical development. By this I mean, for example, that bit shifting would be an analog for multiplication or division.

I knew the answer to this problem immediately, simply because I had saw the no-temp-swap idea mentioned before. I doubt I would have solved this problem without having previously known the answer. I am positive that this shows absolutely nothing about myself as far as problem solving goes. Its a bad interview question.
GnuVince
GnuVince

a = 10
b = 5

a, b = b, a


Thank you Python
jperalta
jperalta
Or if you happen to be deranged and enjoy writing in stack-based languages (such as MSIL) you can do:

ldloc 0
ldloc 1
stloc 0
stloc 1

i.e.

push a
push b
pop a
pop b

[edited by - jperalta on April 27, 2004 9:42:43 AM]
civguy
civguy
a = 10;
b = 5;

std::swap(a, b);

..That wasn't hard :/

[edited by - civguy on April 27, 2004 9:56:36 AM]
jamessharpe
jamessharpe
quote:
Original post by civguy
a = 10;
b = 5;

std::swap(a, b);

..That wasn''t hard :/

[edited by - civguy on April 27, 2004 9:56:36 AM]


In fact your answer may be wrong - by using std::swap you may be implicitly using a local variable it all depends upon the particular implementation of the std library.
grekster
grekster
a= 5, b= 10

a=a*b
b=a/b
a=a/b

different way to whats been mentioned although it isnt as good i dont think
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
CloudNine
CloudNine
In assembly:


MOV eax,[ a ]
MOV ebx,[ b ]
XCHG eax,ebx


The docs for XCHG:

A.168 XCHG: Exchange

XCHG reg8,r/m8 ; 86 /r [8086]
XCHG reg16,r/m8 ; o16 87 /r [8086]
XCHG reg32,r/m32 ; o32 87 /r [386]


XCHG r/m8,reg8 ; 86 /r [8086]
XCHG r/m16,reg16 ; o16 87 /r [8086]
XCHG r/m32,reg32 ; o32 87 /r [386]


XCHG AX,reg16 ; o16 90+r [8086]
XCHG EAX,reg32 ; o32 90+r [386]
XCHG reg16,AX ; o16 90+r [8086]
XCHG reg32,EAX ; o32 90+r [386]

XCHG exchanges the values in its two operands. It can be used with a LOCK prefix for purposes of multi-processor synchronisation.

XCHG AX,AX or XCHG EAX,EAX (depending on the BITS setting) generates the opcode 90h, and so is a synonym for NOP (section A.109


[edited by - CloudNine on April 27, 2004 11:16:05 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.