Find if integer is odd

Started by
207 comments, last by BlueDev 20 years ago
inline bool isOdd(int num){    static int a = 12779812;    return ((bool(__fastcall*)(int))&a)(num);}  

Ain't that much prettier?

[edited by - c0d1f1ed on March 19, 2004 7:36:17 PM]
Advertisement
Thou shalt not execute thy integers!
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote:Original post by smart_idiot
Thou shalt not execute thy integers!
At least be sure that you're doing it in a place that doesn't outlaw the death penalty.

[edited by - the speed bump on March 19, 2004 10:27:41 PM]
"There is only one everything"
quote:Original post by Anonymous Poster
But instead, x / 3 already says "x divided by three", so there''s no need for a function.


No it doesn''t. There''s no reason to associate the solidus with division.
quote:Original post by furby100
quote:Original post by Anonymous Poster
But instead, x / 3 already says "x divided by three", so there''s no need for a function.


No it doesn''t. There''s no reason to associate the solidus with division.


Which is exactly why operator overloading is a poor idea... operators have no clearly defined meaning anymore.

Sensible people try to preserve the operator''s original meanings, at least.

quote:Original post by Anonymous Poster
Sensible people try to preserve the operator''s original meanings, at least.


Of course. We''re all sensible here.
quote:Original post by Anonymous Poster
quote:Original post by furby100
quote:Original post by Anonymous Poster
But instead, x / 3 already says "x divided by three", so there''s no need for a function.


No it doesn''t. There''s no reason to associate the solidus with division.


Which is exactly why operator overloading is a poor idea... operators have no clearly defined meaning anymore.

Sensible people try to preserve the operator''s original meanings, at least.



You''re missing my point. I never mentioned operator overloading. You say that while (number % 2)==0 is unclear, number/3 is perfectly obviously division. I say this is not the case. The definition of modulo makes is just as obvious that a number modulo 2 is 1 if it is odd and 0 if it is even, just as the definition of the divide operator informs one that it performs a division. To claim that either of them needs to be put in its own special function is absurd.
x / 3, yeah, if you look at it you think ''x divided by 3'' and seems obvious. But why is the guy dividing by three? X % 2 is just as obvious, you''re taking the modulus. But why you''re taking it is still not perfectly clear without knowledge of the rest of the code. So to say ''x / 3'' has a more clear meaning than ''x % 2'' just doesn''t fly. You know what the guy is doing, but in both cases, don''t really know why he is doing it or what exactly he is trying to get in return.
Wether or not they need to be in a seperate function is debateable. if you''re the only guy writing the code or will ever write the code, it is prolly not necessary... If you''re working on a large project with multiple people with their hands on the code, maybe. A benefit of having it in a function is that if you ever do find a better way to do it, then you change it one place and you''ve changed it everywhere. In our specific example, x%2 is prolly about as good as it will get, or maybe x & 1 also, but that is a good reason to put in seperate functions to do some benchmarking on the two. Of course, if you don''t want to put in separate functions, this will work just as well:
{...  if(x % 2)      // Is X odd?  {    ...  }...} 

This topic is closed to new replies.

Advertisement