How to do adding/subtracting with bitwise operators

Started by
43 comments, last by MrEvil 19 years, 7 months ago
return (int)&((char *) val)[1];
Advertisement
Thread closing! It is off topic to ask for help with job interview questions or tests!
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
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!
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
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...
int add1(int val) { return -~val; }

...Whaddaya think? ;)
- Hai, watashi no chichi no kuruma ga oishikatta desu!...or, in other words, "Yes, my dad's car was delicious!"
But you were not supposed to use any + or - signs.
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
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
~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
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
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by O_o
......


Cheers :)
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!

This topic is closed to new replies.

Advertisement