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

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

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

Post 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
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post 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
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post 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.
ImageImage
Image
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post 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)".
Last edited by Dwedit on Sat 21 Jul, 2007 10:09 pm, edited 1 time in total.
You know your hexadecimal output routine is broken when it displays the character 'G'.
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

@Dwedit: :oops: I'm sooooo ashamed :oops: ...
ofcourse -2 resets the wrong bits...
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post 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.
You know your hexadecimal output routine is broken when it displays the character 'G'.
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post 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...
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

Gotta love VB and using the same keyword/symbol for more than one thing depending on context in an ambiguous manner...
ImageImage
Image
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post 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.
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post 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. :)
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post 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.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

VB is one weird language.. From VB8 it's only a short jump to C# which I would recommend.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post 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.
Post Reply