Results 1 to 9 of 9

Thread: Mod Operation!?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    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!

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    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!

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mod Operation!?

    He's building a binary representation. Review your lessons on binary to decimal and vice-versa 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.

  5. #5
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    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.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Mod Operation!?

    Quote 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.

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mod Operation!?

    Quote 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.

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

    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
  •  



Click Here to Expand Forum to Full Width