Valhalla Legends Forums Archive | Visual Basic Programming | best way to find out if x is a even or odd number?

AuthorMessageTime
Tontow
what is the best way to find out if x is a even or odd number?
September 26, 2004, 3:52 PM
UserLoser.
[code]
IsOdd = (Number Mod 2)
[/code]

If if the number is odd, it returns 1, otherwise, 0
September 26, 2004, 4:34 PM
St0rm.iD
Or you could do:

[code]
IsEven = Not (Number Mod 2)
[/code]

;)
September 26, 2004, 4:53 PM
K
That seems like a really slow way to do it.  You're doing division and checking the remainder.  Why not

[code]
If Number And 1
'  odd.
End If
[/code]
September 26, 2004, 9:33 PM
St0rm.iD
K just owned us.
September 27, 2004, 12:09 AM
Grok
Don't be so sure.  Depends on how you run it.

1 billion iterations:

Option Explicit

[code]Private Sub cmdTest_Click()
   
    Dim T1 As Variant
    Dim T2 As Variant
    Dim lPos As Long, lMax As Long
    Dim Number As Long
    Dim IsOdd As Boolean, IsEven As Boolean
    Dim test As Long
   
    Number = 8391
    test = 2
   
    lMax = 1000000000
    T1 = Now
    Select Case test
    Case 0
        For lPos = 1 To lMax
            IsOdd = (Number Mod 2)
        Next
    Case 1
        For lPos = 1 To lMax
            IsEven = Not (Number Mod 2)
        Next
    Case 2
        For lPos = 1 To lMax
            If Number And 1 Then
                IsOdd = True
            End If
        Next
    End Select
    T2 = Now
    txtTime.Text = Format(DateDiff("s", T1, T2), "0") & " s"
   
End Sub[/code]

Interpreted:
Case 0:  41 seconds
Case 1:  45 seconds
Case 2:  63 seconds

Compiled (p-code):
Case 0:  40 seconds
Case 1:  37 seconds
Case 2:  18 seconds

Compiled (machine code):
Case 0:  1 seconds
Case 1:  1 seconds
Case 2:  1 seconds

Need a more accurate timer to figure out speed when compiled to machine code.
September 27, 2004, 5:49 AM
K
My premise was that a bitwise and is cheap, while a division operation and checking the remainder is (more) expensive.  Does it really matter if that doesn't apply when running inside the VB IDE?
September 27, 2004, 6:40 AM
St0rm.iD
[quote]
Compiled (p-code):
Case 0:  40 seconds
Case 1:  37 seconds
Case 2:  18 seconds
[/quote]

How'd that happen?
September 27, 2004, 7:09 PM
Grok
[quote author=K link=topic=8856.msg82249#msg82249 date=1096267234]
My premise was that a bitwise and is cheap, while a division operation and checking the remainder is (more) expensive.  Does it really matter if that doesn't apply when running inside the VB IDE?
[/quote]

It might matter to someone testing 5 billion operations from the IDE :)
Seriously though, no one was complaining about your contribution, which provided a better answer.  I was merely testing the scenarios and posting the results.

$t0rm:  TheMinistered might be able to answer that.
September 27, 2004, 8:04 PM
K
What I want to know is how
Not (Value Mod 2)
is faster than
(Value Mod 2).
in P-Code.
September 28, 2004, 1:24 AM
Grok
Great question.  I do not know.  One possibility is the load time for the p-code environment accounted for the longer time for test 0.  When I ran the second and third tesst, the program was already loaded.
September 28, 2004, 4:13 AM
JoeTheOdd
Find if its odd using your own method! :)

Asuming that the number is stored in Text1.Text, the Firing button is Command1, and you have a label called label1, use this code.[code]Private Sub Command1_Click()
Start:
Label1.Caption = "Working"
If Val(Text1.Text) > 1 Then
    Text1.Text = Val(Text1.Text - 2)
    Goto Start
Else
    If Text1.Text = "0" Then MsgBox "Even"
    If Text1.Text = "1" Then MsgBox "Odd"
    Goto Finish
End If
Finish:
Label1.Caption = "Idle"
End Sub
[/code]

Going 2 by 2, this will take insanely long when large numbers are used. I will modify this code to work by 10,000s, 1,000s, 100s 10s, 8s, 6s, 4s, and 2s.
September 28, 2004, 3:45 PM
K
That's an...uh...intriguing way to do it.
September 29, 2004, 12:19 AM
Magickian
One can also bitshift then test for 0, but this is similar to and'ing and more of a pain (since you need to know the size of your data), just another method to do the same thing.
September 29, 2004, 3:08 AM

Search