Code archives/Algorithms/Another IsOdd() function.

This code has been declared by its author to be Public Domain code.

Download source code

Another IsOdd() function. by dangerdave2004
I put this here not to snub Edzup's post, but show that there is usually more than one way to do something.

All that is needed to see if a number is odd is check the the lowest bit of the number:
result = number And 1
--------------------------------
In fact, I would just inline it instead of having all of the function overhead.
Example:
If IsOdd(number) then ...
would be:
If (number And 1) then ...
print IsOdd(97543)

function IsOdd(x)
  return x And 1
end function

Comments

Genexi22004
Either its me or Blitz doesnt use "And" as a math operator at all, and instead for statement usage.

Anyway, was bored and came up with another way of doing it :
(decided to use select since they're faster than mod)

Print IsOdd(975471)

Function IsOdd(x)

	y$ = Right(Str(x),1)

	Select y
		Case 0 : Return 0
		Case 1 : Return 1
		Case 2 : Return 0
		Case 3 : Return 1
		Case 4 : Return 0
		Case 5 : Return 1
		Case 6 : Return 0
		Case 7 : Return 1
		Case 8 : Return 0
		Case 9 : Return 1
	End Select
	
End Function

WaitKey()



Michael Reitzenstein2004
Your function is both longwinded and an order of magnitude slower, Genexi. And is a (single) native CPU operation, string operations are extremely slow in comparison.


Rob Farley2004
And another one... OK, not elegant either, but stressing the point of several ways of doing the same thing.
Print IsOdd(97541)

Function IsOdd(x)
  Return Floor((x+1)/2)-Floor(x/2)
End Function



Michael Reitzenstein2004
OK, not elegant either, but stressing the point of several ways of doing the same thing.

Yeah, there are often multiple ways to do the same thing, except in this case one of them is so superior to the others, it isn't funny!


Michael Reitzenstein2004
OK, not elegant either, but stressing the point of several ways of doing the same thing.

Yeah, there are often multiple ways to do the same thing, except in this case one of them is so superior to the others, it isn't funny!


Dreamora2004
It's both far more work than needed in the worst case:

Function IsOdd(x)
  return x mod 2
end function



Floyd2004
(x And 1) is clearly the preferred method.

The one with strings is extremely inefficient and the one with Floor is just plain wrong.


Michael Reitzenstein2004
Dreamora - that one does *exactly* the same thing as the original function, but I believe And will be significantly faster (if Blitz doesn't optimise the const Mod into an And).


Dreamora2004
I know
But it's a very good method for other stuff too, not only the even-odd check.
But it needs some math knowledge to find out how usefull and powerfull mod really is ...

And is very usefull if you use bitflags. for stuff like that there is no more efficient way :)


Michael Reitzenstein2004
And is very usefull if you use bitflags. for stuff like that there is no more efficient way :)


What, If Var And %10 doesn't work? :)


Dreamora2004
then you can use connected mod checks :)


not (Var mod 2) and not (Var mod 4) ...

or you copy the flag and use shr / shl to check the flag


Bug Face2004
Sorry but mod is a very slow way of doing this. The fastest way of achieving the odd is not to do it in a fucntion at all but in line it, and when it is a one-liner, why wouldn't you?

Of course for bitwise AND has the disadvantage that it can only mask to powers of 2, where as mod does it with anything.

Let's just take a moment to remember that a mod is calculated by determining the remainder of a division. I would take steps to organize my data so I didn't have to use a mod and could do it with a bitwise operation.

I have to give points to the guy who wrote the string one for the most over implemented code I have ever seen. So! In the spirit of optimisation and paying homage to someone who clearly works for microsoft (:>) I have optimised it ;-)

Function isOdd(x)
	Select Right(Str(x),1)
		Case 0,2,4,6,8 : Return 0
		Case 1,3,5,7,9 : Return 1
	End Select
End Function


I have a taste for this now! This would work ;-)

Fucntion isEven(x)
       return (Not x) and 1 
End Function

Function isOdd(x)
       return Not isEven(x)
End Function



Shambler2004
Another useful microsoft function...

Function PlusOne(x)
return x+1
End Function



Floyd2004
And here's the power of leveraged code.

I've always wanted a PlusTwo function.
Now I don't have to worry about the implementation details.

Function PlusTwo(x)
	Return PlusOne( PlusOne( x ) ) 
End Function



AntonyWells2004
And here's the safe mode version of microsoft's entry into this pioneering new field.

Function SafeModeIsOdd_COM_DX_API_OBJECT_ANNOYING_WORDS( value#=1,isValueANumber=possibly)
     if not isValueANumber=true
           return MS_THIS_IS_NOT_A_NUMBER
     else
           ask=input(value+"< What is the fraction>")
           return ask
     endif
end function


The MS motto,
If in doubt, ask the user.


AntonyWells2004
-Double Post.


Bug Face2004
I cannot thank the later posters enough... In the interests of code re-use, I have managed to remove an additional isEven function from my ealier post

Function PlusOne(x)
return x+1
End Function

Function isOdd(x)
    return (Not PlusOne(x)) And 1
End Function



Michael Reitzenstein2004
Actually, that would be:

Function PlusOne(x)

Return x+1

End Function



Function isOdd(x)

    Return (~PlusOne(x)) And 1

End Function



Michael Reitzenstein2004
Actually, that would be:

Function PlusOne(x)

Return x+1

End Function



Function isOdd(x)

    Return (~PlusOne(x)) And 1

End Function



jfk EO-111102005
Funny thread, anyway

Print IsOdd(97541)

The calling of a function that forces the use of a stack with Return adress, Parameter and Return-value handover is such an overhead.

When you are a Hardcore Coder :) then you will recognize something like

print (97541 and 1)

as isOdd all the time.


aindovin2006
=) i'm happy because i think i came up with a fun new way of doing this.


Print isodd(15) ; test of an odd number
Print isodd(10) ; test of an even number
print isodd(97541) ; test of your number?


Function isodd(value) ; our odd testing function
testnum%=((-1)^(value)) ; the way this works is we put -1 to our value's power. -1 x 1 = -1 // -1 x 2 = 1 =D OMG THATS INSANE HOW THAT WORKS.
Return testnum% ;
End Function ;


n8r2k2006
http://www.blitzbasic.com/Community/posts.php?topic=44481#495317

I asked a question on this subject a year ago. There are some other ways in there to test for odd/evenness (is that a word)


mm2006
even this entry is odd


WendellM2006
The calling of a function that forces the use of a stack with Return adress, Parameter and Return-value handover is such an overhead.

That is true. In tests, it takes three times as long to do the "And" test in a function compared to doing the test inline. Of course, we're talking 6 millisecs for a million tests compared to 2 millisecs, so it doesn't really matter, but I'll conveniently ignore that... :)

When you are a Hardcore Coder :) then you will recognize something like

print (97541 and 1)

as isOdd all the time.

This can be made easier by using a const:
Const IsOdd = 1

Print (97541 And IsOdd)
At least that hints at what the And is doing. Think of it as the abbreviated (speedo) version of a function (swim trunks): not as comfortable for observers to look at, but faster, and still better than seeing what lies underneath directly.

But if you prefer slowness and deliberate obfuscation, I think aindovin's use of exponentiation wins. :)


n8r2k2006
why the heck was this dragged up again?


WendellM2006
I'd guess because mm found it interesting, funny, or odd.

WhyTheHeckDragUpAgain = Rand(3)


Code Archives Forum