return (int)&((char *) val)[1];
How to do adding/subtracting with bitwise operators
Thread closing! It is off topic to ask for help with job interview questions or tests!
OK. I had a change of heart, since the poster mentioned the problem was time-based and I know that some companies do issue time-based tests.
HOWEVER, I still consider interview help to generally be off topic, and may close future threads!
HOWEVER, I still consider interview help to generally be off topic, and may close future threads!
Armand, I am profoundly impressed with your coworker! I think that's the best solution ever. Though I don't think I could improve upon its efficiency (without using ++, heh) I do think it can be rewritten a little more artistically...
...Whaddaya think? ;)
int add1(int val) { return -~val; }
...Whaddaya think? ;)
what is actually happening with this peice of code :S
int add1(int x)
{
return ~x * 0xFFFFFFFF;
}
could someone please explain to me what the ~x * 0xFFFFFFFF is all about its really confused me:S
int add1(int x)
{
return ~x * 0xFFFFFFFF;
}
could someone please explain to me what the ~x * 0xFFFFFFFF is all about its really confused me:S
~x * 0xFFFFFFFF;
~x is the same as -(x + 1) or a one's complement operation
so it is -(x + 1) * 0xFFFFFFFF
0xFFFFFFFF = -1
so it is -(x + 1) * -1 which is x + 1
~x is the same as -(x + 1) or a one's complement operation
so it is -(x + 1) * 0xFFFFFFFF
0xFFFFFFFF = -1
so it is -(x + 1) * -1 which is x + 1
wouldnt doing that multiply be more expensive than just using an actual add?
infact all the solutions are probably more expensive
so whats the point of them asking a question like that?
they want programmers who can write obfuscated code or something?
sure, having someone who is intelligent is important to any job, but shouldn't it be a more Useful demonstration of intelligence?
nothing against anyone or the clever answers theyve come up with; but id be a little annoyed at getting asked that kind of question for a job
its like one of those fake situtation word problems they give you in school; where the question is so contrived that its useless outside of that classroom
infact all the solutions are probably more expensive
so whats the point of them asking a question like that?
they want programmers who can write obfuscated code or something?
sure, having someone who is intelligent is important to any job, but shouldn't it be a more Useful demonstration of intelligence?
nothing against anyone or the clever answers theyve come up with; but id be a little annoyed at getting asked that kind of question for a job
its like one of those fake situtation word problems they give you in school; where the question is so contrived that its useless outside of that classroom
Quote:Original post by haphazardlynamed
... so whats the point of them asking a question like that? ...
They want to know how familiar you are with the bitwise operations -- that kind of knowledge is important in game programming. I sometimes ask this question in interviews.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement