|
I've been asked this one in an interview.
I wasn't aware of the XOR swap at the time and I think I used a temp variable in my answer.
I don't think I got an offer from that interview.
Pete
|
|
|
|
|
I have been a couple of times as well - I'm a bit wary of taking any job that needs that sort of low level bit fiddling though. A good one to fire back at the interviewer is "I'll show you the C, you show me the boolean algebra proof." It's a good test of what the interviewer is like.
Cheers,
Ash
|
|
|
|
|
lgmanuel wrote: am trying to swap the values of 'x' and 'y' without using a temporary storage like 'z'..
why ?
IMO, other than the fact that it uses a small "hack", there is no reason to use that in 2010. (or as a stupid interview question).
Watched code never compiles.
|
|
|
|
|
consider this:
template<class A>
void xswap(A& a, A& b)
{
for(size_t i=0; i<sizeof(a); ++i)
{
((char*)&a)[i] ^= ((char*)&b)[i];
((char*)&b)[i] ^= ((char*)&a)[i];
((char*)&a)[i] ^= ((char*)&b)[i];
}
}
This works even if A is a class one gigabyte wide, without allocate an extra gigabyte.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
...and who would ever handle such monster objects by value?
|
|
|
|
|
Depends on what the object represent: may be transfer buffers, one of which is shared with a DMA or another process.
"Swap" merely means exchange the data visibility between devices, or processors.
And since they are physically bounded to a device, cannot be swapped by reference (swapping the respective pointers).
With less dimensions (typically, up to 4K or 9K), this exchanges are typically operated between the switch processors and the route processors of routers.
Of course, is not something to be abused, but may have its own specific application.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
He doesn't. Note the signature: "xswap(A& a". But there are a number of flaws in handling things this way. For starters, the Visual C++ compiler (and I presume everyone else will also be in this category) compiles two versions for xswap, even if the only difference is the size of the array, so
int a[1024], b[1024], c[2048], d[2048];
xswap(a, b); xswap(c, d);
produces two copies of xswap. But let's not stop there. The assembly output for the xor version is horrible. I didn't check the + and - version above, but it will probably be just as bad. Visual C++ (and presumably other compilers also) does what looks like the right thing if you use a simple temporary, namely something like this:
mov eax, [current ptr for a]
mov edx, [current ptr for b]
mov [current ptr for b], eax
mov [current ptr for a], edx
Temporaries are definitely the way to go. They're more susceptible to compiler optimizations such as coalescing, and if you prefer, some style of XMM hand optimization. I see pxor has the option for an m128 argument, so maybe there's something there. I'm not even sure why this would be an interview question. Maybe a better interview question is: In how many ways is the xor solution a bad idea?
|
|
|
|
|
I was not referring to the parameter passing, but to the need for swapping gigabytes of data in-place instead of swapping pointers to buffers, which seems like the most obvious choice in most cases.
You make very good points in your post. Thanks for that.
|
|
|
|
|
i think its a homework question.
|
|
|
|
|
Using Ruby:
x, y = y, x
Try not to take life to seriously.
When all is done no one gets out alive anyway.
|
|
|
|
|
Use the processor registers. No temp variable involved. Use assembly language of your choice.
Fast! 4 CPU instructions w/4 memory waits & you're done!
int x = 150;
int y = 45;
asm{
LDA x;
LDB y;
STA y;
STB x;
}
Using XOR, do it 3 times slower...
11 CPU instructions w/7 memory waits
int x = 150;
int y = 45;
x ^= y;
y ^= x;
x ^= y;
#DEFINE B 1
int x = 150;
int y = 45;
asm{
LDA x;
XOR y;
STA x;
STA B;
LDA y;
XOR B;
STA y;
STA B;
LDA x;
XOR B;
STA x;
}
Otherwise, this other SLOW method...
12 CPU instructions w/6 memory waits
x += y;
y = x - y;
x -= y;
#DEFINE B 1
int x = 150;
int y = 45;
asm{
LDA x;
LDB y;
ADA B;
STA x;
CMB;
INB;
ADA B;
STA y;
CMA;
INA;
ADA x;
STA x;
}
Notes for the unknowing:
Operations on registers are much faster than memory access
Negating a number is two's complement
Two's complement is one's complement +1
One's complement reverses (NOTs) all bits
Who loves Assembler???
No JITCHIT either.
Gary
|
|
|
|
|
Is this in the "Homework help" section? the only time i've ever needed to do this was CompSci 221. haha.
|
|
|
|
|
These kinds of interview questions are rather silly. Now really: what does it prove or show? Maybe that you know how to perform the swap using some weird trick, but as far as your abilities i.t.o. software design it shows nought.
It will not work on strings, anyway.
|
|
|
|
|
int x = 450;
int y = 45;
Console.WriteLine("X = " + y);
Console.WriteLine("Y = " + x);
If you ask me, this fulfils the spec.
Edit: oh and this works with strings, numbers, pointers (well maybe not if it's C#), or any datatype at all really.
Ninja (the Nerd)
Confused? You will be...
|
|
|
|
|
You should be aware of the XCHG assembler instruction which will exchange two values, uses 3 cpu clocks. Your optimizations will, in some cases, use 3x more CPU clocks, larger binary and unreadable code.
|
|
|
|
|
<joke>
to avoid using temporary storage - i would consider showing the user two boxes, and asking them to swap the numbers for me. this is safe for strings or XML or whatever. if the user chooses to use Notepad or something, it doesn't bother me, but i know that my code executes as fast as possible and is bug-free because i am using natural techniques from nature.
microsoft does this when you have to change your password. it shows you three text boxes. the first is your old password, and the next two are your new password. the code first checks that the new password matches the new new password, and then swaps the new new password for the old password.
works seamless every time
|
|
|
|
|
x^=y;
y^=x;
x^=y;
Yes, it's cute, but it's faster to use a temporary. The xor method is only useful if you're doing it in assembly language and you don't have a free register or a temporary memory location.
ken@kasajian.com / www.kasajian.com
|
|
|
|
|
int x=150;
int y=45;
x=x+y;(195)
y=x-y;(150)
x=x-y;(45)
|
|
|
|
|
x = 15
y =13
x=x + y (15 + 13) = 28
y = x- y (28-13) = 15
x = x-y(28-15)
x = 13
Hence x=13;
y=15.
jigar sheth
|
|
|
|
|
hello guys.....im new to this. I wanna know that how can I get started with waveInOpen?? I am getting confused. What is the right path to get used to it. I just can not be happy taking samples from Internet, I must learn it but not taking it on the big picture, what is the right place.......plz plz help me.
|
|
|
|
|
|
Hi,
i m taking help of this article for design outlook style
Outlookbar-style menu interface[^]
i m using COutlook2Ctrl::OCL_SELECT for trigger with assign Menu Item,in menu item this is disable by CCmdUI,
but please tell me how can i disable this purticular subitem in this outlook tree.
please help me for this.
thanks in advance.
|
|
|
|
|
Generally,singleton pattern is applied by the way that the ctor is private and gives a member funtion named getInstance, but CWinApp's instance is defined as a global object,its ctor is not private.So how MFC realizes the singleton pattern for CWinApp?
|
|
|
|
|
The CWinApp class is not a singleton class.
It doesn't need to be because MFC needs this class has to be created before it internally calls WinMain .
If it was designed as a singleton, you would be able to create it only after WinMain in called and also you would need to write WinMain yourself.
That is why you create a global instance of it and you must create only one as the documentation says.
This is as easy as saying that you can only have one main or WinMain function.
|
|
|
|
|
I Made a test .If i instance another global CWinApp descendant class.In release mode ,there is only one instance in the application ,but not two.How does MFC keep only one CWinApp descendant instance in the application?
|
|
|
|