Page 1 of 1

[visual basic] How to check bit 0 of an integer variable

Posted: Sat 21 Jul, 2007 5:20 pm
by driesguldolf
Currently I need to check if a number is even or odd
in assembly this is VERY easy:

Code: Select all

   rrca
   jp c, odd
   jp nc, even
I tought you needed to do something similar in visual basic:

Code: Select all

   if (newint and -2)=0 then even() else odd()
Don't you expect this to be correct? Very slim chance it's an error in other parts of my code, I'll do some testing...

EDIT: I have a strong suspicion that it will convert newint and -2 to booleans and then compare them, wich obvious most of the time (always?) gives 0... Any trick to get around that?

EDIT2: My very stupid work around:

Code: Select all

    Public Function CheckBit0(ByVal nr As Integer) As Integer
        Math.DivRem(nr, 2, CheckBit0)
    End Function

Posted: Sat 21 Jul, 2007 5:38 pm
by King Harold
well I'm not really into VB, but in some other languages..
if( X%2 == 0 ) then even() else odd() //this basically checks whether X is divisible by 2
if( X&1 == 0 ) then even() else odd() //this basically checks bit 0
if( 2*(X/2) == X ) then even() else odd() //this relies on integers to be exactly that - integers, thus bit0 is lost when dividing
note that you may not need "== 0" depending on how type-save your language is..
Oh and remember that ( brackets ) go before (casting) in precedence

Posted: Sat 21 Jul, 2007 6:45 pm
by Andy_J
As an alternative, you could abuse integer division:

Code: Select all

If (nr / 2) = (nr \ 2) Then
 ' Number is even
Else
 ' Number is odd
End If
I think the And you are thinking of is & in VB like any other language (but it's been years since I've used VB and may be wrong). "And" is &&.


Edit: Last I checked, == is a syntax error in VB. = is used for comparison as well as assignment. % may work for modulus, I forget.

Posted: Sat 21 Jul, 2007 10:02 pm
by Dwedit
The AND operator in all versions of VB is bitwise.
So when you need to check bit 0 of a variable, just use "(variable and 1)".

Posted: Sat 21 Jul, 2007 10:05 pm
by driesguldolf
@Dwedit: :oops: I'm sooooo ashamed :oops: ...
ofcourse -2 resets the wrong bits...

Posted: Sat 21 Jul, 2007 10:15 pm
by Dwedit
Anding with -2 is the 'bit clear' operation. Since bit clearing and ANDing are highly related, I can see someone accidentally confusing them.
To clear bit 0 in VB, use (variable and (not 1)). 'Not 1' becomes -2.

Posted: Sat 21 Jul, 2007 10:41 pm
by driesguldolf
nah, I think I shouldn't have made a topic about something trivial like that...
I've such errors before like confusing 'rra' and 'rla' in a decompression routine... Gave me weird results and nightmares...

Posted: Sat 21 Jul, 2007 11:15 pm
by Andy_J
Gotta love VB and using the same keyword/symbol for more than one thing depending on context in an ambiguous manner...

Posted: Mon 23 Jul, 2007 7:51 am
by Halifax
In my VB class we used

If var mod 2 = 0 Then
...
Else
...
End If

I never learned bitwise operations for VB so that there may be very unoptimized.

Posted: Mon 23 Jul, 2007 2:19 pm
by Andy_J
Aha, that's right, I should have remembered VB uses words for just about everything. I'm surprised it doesn't use Plus, Minus, Times, Divided By. :)

Posted: Mon 23 Jul, 2007 3:52 pm
by benryves
Visual Basic 7 and above added two keywords for lazy evaluation - AndAlso ("&&") and OrElse ("||"). And and Or are not lazy, for backwards compatibility reasons.

AndAlso and OrElse seem to be described backwards, to me. Both sound like they'd check both sides of the expression.

Posted: Mon 23 Jul, 2007 5:55 pm
by King Harold
VB is one weird language.. From VB8 it's only a short jump to C# which I would recommend.

Posted: Mon 23 Jul, 2007 6:30 pm
by CoBB
benryves wrote:AndAlso and OrElse seem to be described backwards, to me. Both sound like they'd check both sides of the expression.
These names seem to have come from ML, where they are the only lazy constructs.