
Oct 14th, 2007, 11:29 PM
#1
Thread Starter
New Member
Mod Operation!?
Hi there i am pretty new to visual basic and I am doing a Decimal to Binary conversion. My teacher showed me this code the other day and was just wondering if someone could give me some clarification as to what exactly the MOD operator is doing:
Private Function Dec2Bin(ByVal y As Long) As String
Do While y > 0
If (y Mod 2) Then
Dec2Bin = "1" & Dec2Bin
Else
Dec2Bin = "0" & Dec2Bin
End If
y = y \ 2
Loop
Thanks!

Oct 14th, 2007, 11:36 PM
#2
Re: Mod Operation!?
Mod returns the remainder. If y was 5, 5 divided by 2 is 2 remainder 1 (rather than 2.5). Review your decimal to binary conversion lessons first, then refer to source above code.
Actually the if else construct is unnecessary since division by 2 either results in a remainder of 1 or its an even number.
Last edited by leinad31; Oct 14th, 2007 at 11:39 PM.

Oct 15th, 2007, 01:12 AM
#3
Thread Starter
New Member
Re: Mod Operation!?
Thanks for the response leinad31!
Alright i mostly figured that part out but the part that i do not get at all is:
If (y Mod 2) Then
Dec2Bin = "1" & Dec2Bin
Else
Dec2Bin = "0" & Dec2Bin
Whats is the & doing with 1 <> Dec2Bin?
Thanks!

Oct 15th, 2007, 01:23 AM
#4
Re: Mod Operation!?
He's building a binary representation. Review your lessons on binary to decimal and viceversa conversions first.. if you'll do so, you'll realize that the code provided has errors. Learn the fundamentals in order to have basis for analysis.

Oct 15th, 2007, 02:56 AM
#5
Frenzied Member
Re: Mod Operation!?
The & operator will append two strings together.
In this case it will add the value of the currently tested bit to the result already in Dec2Bin.

Oct 15th, 2007, 04:18 AM
#6
Re: Mod Operation!?
Yes concatenation is performed and theres a recursive call to Dec2Bin... but then why loop if the implementation is recursive? Either you get the bits (1s and 0s) through the loop or through recursion.
There are still other errors, take this as an opportunity to train/exercise your debugging skills.

Oct 15th, 2007, 08:25 AM
#7
Re: Mod Operation!?
Originally Posted by leinad31
Yes concatenation is performed and theres a recursive call to Dec2Bin...
It's not actually recursive, it just looks like it is. Bearing that in mind the code does work as intended, although perhaps not as efficient as it could be.
Edit: Here's a recursive version...
Code:
Private Function Dec2Bin(ByVal y As Long) As String
If y <> 0 Then Dec2Bin = Dec2Bin(y \ 2) & CStr(y Mod 2)
End Function
Last edited by Milk; Oct 15th, 2007 at 08:35 AM.

Oct 15th, 2007, 08:45 AM
#8
Re: Mod Operation!?
Originally Posted by Milk
It's not actually recursive, it just looks like it is. Bearing that in mind the code does work as intended, although perhaps not as efficient as it could be.
I thought there was still an error. jeroen my mistake. As what just happened, that statement can be mistaken as a recursion if other party is used to always passing arguments to functions with parameters or is used to using working variables.

Oct 15th, 2007, 09:15 AM
#9
Re: Mod Operation!?
In VB, inside a function you can use the function name as if it was a variable. The value you assign to the function name, inside the function, is the value that is later returned.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
