Skip to main content
gamedev.net
🔒 Locked

Even/Odd

Started by Thrump 5 replies 200+ views
Original Post
Thrump
Thrump
In my code I had this to test if something was odd
if(integer&1)
 
which didn''t work.
if(integer%2)
 
did work. Why?!
Thrump
Thrump
Jeez that''s tiny...


if(integer&1)

if(integer%2)
Tornado
Tornado
That''s strange.
Integer & 1 = Integer % 2

You sure that Integer is not a floating point in your app?



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Thrump
Thrump
It''s an unsigned integer.
I''ll do some more testing and get back to you.
fifty1
fifty1
This PROBABLY won''t make any difference, but when I''m doing bitwise operations I like to explicity define them as unsigned ints:

if (integer & 0x01) ...

But I can''t see why (integer & 1) wouldn''t work.
alexmoura
alexmoura
I could seen it not workin in the unlikely even that the compiler though that 1 was not a same size integer, but that would be very strange indeed. try the 0x00000001 sugestion
and tell us what is going on...

You know, I never wanted to be a programmer...

Alexandre Moura
Thrump
Thrump
I kind of forgot about this problem, as I got it working with using modulus. The programs gone through several iterations, and now that I exchange them, it seems to work.

I think this is another fine example of me being a dumb programmer jumping to conclusions...

Topic Locked

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

Sign in to reply to this topic.