Odd or Even

  • Thread starter Thread starter William Foster
  • Start date Start date
W

William Foster

Good evening all,

Does anyone know how to find out whether a value is either odd or even
in the Visual Basic component of Visual Studio 2005. I have found the
Math and Information tools which provide some of the common functions
within other Microsoft packages such as Excel; however basic functions
like Odd and Even elude me.

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster
 
Ken,

Thank you for the quick reply, you have no idea how many silly
references there are to odd and even within the Visual Studio help, it
was driving me quite mad.

Yours sincerely,

William Foster
 
William Foster said:
Does anyone know how to find out whether a value is either odd or even
in the Visual Basic component of Visual Studio 2005. I have found the
Math and Information tools which provide some of the common functions
within other Microsoft packages such as Excel; however basic functions
like Odd and Even elude me.

\\\
If i Mod 2 = 0 Then
MsgBox("Even.")
Else
MsgBox("Odd.")
End If
///
 
Herfried,

Thanks for your idea, Ken nailed the solution I needed first go though.

I should have written the question a couple of hours ago instead of
getting mad at the endless pages of help on functions that don't exist
in this suite.

Yours sincerely,

William Foster
 
or simply

Public Function IsOdd(value as Integer) as boolean
Return CBool(value And 1)
End Function

*guy*
 
What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true, since
value is always = value and 1 is always = 1.

The Modulus (Mod) mathematical operand is designed to divide 2 numbers and
return the remainder, so taking a number and using mod 2 will indicate if
you have an even number if there is no remainder.
 
What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true,
since value is always = value and 1 is always = 1.

Huh? He was testing if the LSB (Least Significant Bit) was set.
 
hi scott, and is used here as a bit operator

basically take two integers and the result has a bit set if the same bit is
set in both operators

with a logical 'Or' the result has a bit set if a bit is set in either
operator, see also Xor, Not ....

hth
*guy*
 
Scott M. said:
What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true,
since value is always = value and 1 is always = 1.

F1, 'And' ;-).

'And' is a binary operator which will use a binary AND to combine both
'value' and '1'. The result is converted to a 'Boolean'.
 
Ahhh!

Thanks.


guy said:
hi scott, and is used here as a bit operator

basically take two integers and the result has a bit set if the same bit
is
set in both operators

with a logical 'Or' the result has a bit set if a bit is set in either
operator, see also Xor, Not ....

hth
*guy*
 
Steve said:
Or before mod was invented...

if int(value)/2=value/2 then isven=true

-sw

You might get problems with that because 2=2.0 can result in false.
Therefore I've always used bit comparison like guy posted.
 
I think a more appropriate method would be this:

Public Function IsOdd(value As Integer) As Boolean
Return ((value And 1) = 1)
End Function
 
Back
Top