PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Check if number is odd or even-VBForums
Results 1 to 19 of 19

Thread: Check if number is odd or even

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2002
    Posts
    33

    Check if number is odd or even

    I use this code

    VB Code:
    1. Function IsOdd(number as integer) as boolean
    2.     Dim tNum as integer
    3.     tNum=right(number,1)
    4.     if tNum=0 or tNum=2 or tNum=4 or tNum=6 or tNum=8 or tNum=10 then
    5.         IsOdd=false
    6.     else
    7.         IsOdd=true
    8.     end if
    9. end function

    But are there any better way to check if it's odd or even?, because I need to call it thousand times in each second

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Check if number is odd or even

    VB Code:
    1. Private Sub Command1_Click()
    2. If CLng(Text1.Text) Mod 2 > 0 Then
    3.     MsgBox "Odd Number"
    4. Else
    5.     MsgBox "Even Number"
    6. End If
    7. End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Check if number is odd or even

    or...
    VB Code:
    1. Function IsOdd(ByVal iNum As Integer) As Boolean
    2.     IsOdd = ((iNum \ 2) * 2 <> iNum)
    3. End Function

  4. #4
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: Check if number is odd or even

    Or you could try this.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim intNumbetToCheck As Integer
    5.  
    6.     intNumbetToCheck = CInt(InputBox$("Enter An number to check if it is odd or Even"))
    7.  
    8.    
    9.     If IsOdd(intNumbetToCheck) Then
    10.         MsgBox "you input was odd"
    11.     Else
    12.         MsgBox "Your input was even"
    13.     End If
    14. End Sub
    15.  
    16.  
    17. Private Function IsOdd(ByVal oddNumber As Integer) As Boolean
    18.     IsOdd = oddNumber And 1
    19. End Function

    Hope this helps!!


  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,873

    Re: Check if number is odd or even

    The proper way to check if a number is odd is to see if the "1" bit is set.

    Code:
    dim lngValue as long
    
    lngValue = 3
    
    If lngValue and 1 Then Debug.Print lngValue & " is odd"
    
    If (lngValue and 1) = 0 Then Debug.Print lngValue & " is even"
    
    lngValue = 4
    
    If lngValue and 1 Then Debug.Print lngValue & " is odd"
    
    If (lngValue and 1) = 0 Then Debug.Print lngValue & " is even"

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jul 2002
    Posts
    33

    Re: Check if number is odd or even

    Thanks for any comments and the last one work great

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,873

    Re: Check if number is odd or even

    Bombdrop had the same idea - I didn't see that post when I made mine.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Check if number is odd or even

    Quote Originally Posted by szlamany
    The proper way to check if a number is odd is to see if the "1" bit is set.
    What is the "1" bit?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  9. #9
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Check if number is odd or even

    SeanK,

    Are you familiar with binary (or base 2) arithmetic?

    Normal human beings use base 10 (decimal) in which a number is even if it ends in a 0, 2, 4, 6 or 8. Programmers use base 2 (binary). In binary a number is even if it ends in 0.

    If you don't know what I'm talking about maybe this will help:

    Code:
    Number          Decimal                         Binary
                     Tens    Units             Eights   Fours   Twos   Units
        0              0         0               0       0      0      0           0000
        1              0         1               0       0      0      1           0001
        2              0         2               0       0      1      0           0010
        3              0         3               0       0      1      1           0011
        .              .         .               .       .      .      .          ......
        .              .         .               .       .      .      .          ......
        7              0         7               0       1      1      1           0111
        8              0         8               1       0      0      0           1000
        9              0         9               1       0      0      1           1001
       10              1         0               1       0      1      0           1010
    Last edited by trisuglow; Mar 2nd, 2005 at 10:01 AM.
    This world is not my home. I'm just passing through.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Check if number is odd or even

    Quote Originally Posted by SeanK
    What is the "1" bit?
    The least significant bit.
    Code:
    10011101
    ^      ^ Least significant
    |
    Most significant

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,873

    Re: Check if number is odd or even

    Quote Originally Posted by SeanK
    What is the "1" bit?
    The first binary bit is 1, followed by 2, 4, 8, 16, 32 and so on.

    Note that only the first bit is odd - all decimal values that are odd have the "first bit" in common.

    To check if the first bit is set in a longword, you use:

    IF LNGVALUE AND 1 THEN

    To check if the second bit is set you use:

    IF LNGVALUD AND 2 THEN

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Check if number is odd or even

    Quote Originally Posted by szlamany
    The first binary bit is 1, followed by 2, 4, 8, 16, 32 and so on.

    Note that only the first bit is odd - all decimal values that are odd have the "first bit" in common.

    To check if the first bit is set in a longword, you use:

    IF LNGVALUE AND 1 THEN

    To check if the second bit is set you use:

    IF LNGVALUD AND 2 THEN
    Ok. If the first bit is odd, then why is 9458 an even number, but 8459 is an odd number. It seems to me that it is checking the last number (bit?) not the first.

    Also, Joacim Andersson: You posted this in a thread about CAPS LOCK and SHIFT key states. How does this 'high order bit' relate?
    Quote Originally Posted by Joacim Andersson
    Well, the high order bit is 1 if it's been pressed but the low order bit of the return value is always 1 if the key is toggled. The CAPS LOCK key is considered toggled if it's on, so you could use this code:
    VB Code:
    1. Public Function CapsIsOn() As Boolean
    2.     CapsIsOn = ((GetKeyState(vbKeyCapital) And 1) = 1)
    3. End Function
    Last edited by SeanK; Mar 2nd, 2005 at 10:22 AM.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  13. #13
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Check if number is odd or even

    Bits (BInary digiTS) are numbered from right to left. This does make sense - the rightmost bit is always 1 regardless of how many digits there are in the number.
    This world is not my home. I'm just passing through.

  14. #14
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Check if number is odd or even

    Low order bit is the first bit (rightmost).
    The highest order bit is the last bit (leftmost).
    This world is not my home. I'm just passing through.

  15. #15
    New Member
    Join Date
    Jun 2016
    Posts
    2

    Re: Check if number is odd or even

    Private Sub Command1_Click()
    If InputBox("") Mod 2 > 0 Then
    MsgBox "Odd Number"
    Else
    MsgBox "Even Number"
    End If
    End Sub



    the best! way

  16. #16
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Check if number is odd or even

    Not really... this thread is 11 years old.
    and even back then it was suggested the fastest way was masking the 1st bit.

    Code:
    If YourNumber And 1& Then
    MsgBox "Odd Number"
    Else
    MsgBox "Even Number"
    End If
    End Sub

  17. #17
    New Member
    Join Date
    Jun 2016
    Posts
    2

    Re: Check if number is odd or even

    bro doesn't this code seems simple...for basic people..?\


    If InputBox("") Mod 2 > 0 Then
    MsgBox "Odd Number"
    Else
    MsgBox "Even Number"
    End If

  18. #18
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,118

    Re: Check if number is odd or even

    DEXWERX isn't saying that your way isn't simple. He's saying that your claim that it's 'best way' is untrue.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Check if number is odd or even

    Why did you feel the need to dig up an 11 year old thread that already had an answer? Thread closed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width