LoginLogin

Other way to know if your number is even or odd

Root / Programming Questions / [.]

hakkeCreated:
(1 AND n) returns 1 if n is odd So... !(1 AND n) returns 1 if n is even

N<<31 is shorter, but doesn't return 1 for true. EDIT: but use MOD because it's better

Although in this case, AND is probably the best way to do it, MOD will tell you if a number is a multiple and works for any number.
'Number is multiple of 3
!(NUM MOD 3)
'Number is multiple of 11
!(NUM MOD 11)
'Number is not multiple of 7
!!(NUM MOD 7) 'I think double negation works for true/false... right?
If you're trying to find out if something is a multiple of a power of two, the best way is with AND though:
'Number is a multiple of 4
!(NUM AND 3)
'Number is a multiple of 8
!(NUM AND 7)
'Number is not a multiple of 64
!!(NUM AND 63)