Home All Groups Group Topic Archive Search About
Author
21 Oct 2005 7:36 PM
Arpan
The following function converts a decimal number into its binary
equivalent (it gets fired when an OptionButton is checked):

Function Dec2Bin(IntegerNumber As Long)
    Dim iNum
    Dim BinValue, TempValue
    iNum = IntegerNumber
    Do Until iNum = 0
        TempValue = iNum Mod 2
        BinValue = CStr(TempValue) + BinValue
        MsgBox (iNum)
        iNum = iNum \ 2
    Loop

    txtOutput.Text = BinValue
End Function

As such, the above function does the needful but there's one thing
which I couldn't follow. Suppose I enter the decimal number 10 & then
check the OptionButton that fires the above function. When I do so,
iNum becomes equal to 10 & the message box first shows 10.....that's
OK, then the message box shows 5....that's OK as well but after this,
the message box shows 2! This is what I couldn't understand. How come
the message box is showing 2? Shouldn't it be 2.5 instead because when
the variable iNum is 5, then the line just before the Loop line divides
5 by 2 which is 2.5; so why is the message box showing 2 instead of
2.5? Can someone please explain me this?

Thanks,

Regards,

Arpan

Author
21 Oct 2005 7:42 PM
Bob Butler
Show quote Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1129923391.138103.217460@o13g2000cwo.googlegroups.com
> The following function converts a decimal number into its binary
> equivalent (it gets fired when an OptionButton is checked):
>
> Function Dec2Bin(IntegerNumber As Long)
>     Dim iNum
>     Dim BinValue, TempValue
>     iNum = IntegerNumber
>     Do Until iNum = 0
>         TempValue = iNum Mod 2
>         BinValue = CStr(TempValue) + BinValue
>         MsgBox (iNum)
>         iNum = iNum \ 2
>     Loop
>
>     txtOutput.Text = BinValue
> End Function
>
> As such, the above function does the needful but there's one thing
> which I couldn't follow. Suppose I enter the decimal number 10 & then
> check the OptionButton that fires the above function. When I do so,
> iNum becomes equal to 10 & the message box first shows 10.....that's
> OK, then the message box shows 5....that's OK as well but after this,
> the message box shows 2! This is what I couldn't understand. How come
> the message box is showing 2? Shouldn't it be 2.5 instead because when
> the variable iNum is 5, then the line just before the Loop line
> divides 5 by 2 which is 2.5; so why is the message box showing 2
> instead of 2.5? Can someone please explain me this?

"iNum = iNum / 2" would be floating point division and would return 2.5;
since iNum is decalred as a Variant it would store that value

"iNum = iNum \ 2" is integer division; note the slash is backwards...  5\2 =
2

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
21 Oct 2005 8:06 PM
Arpan
Thanks Bob for your help. Didn't notice the "\" which wasn't doing the
normal division!

Thanks once again,

Regards,

Arpan
Author
21 Oct 2005 8:14 PM
Someone
/ divides and returns floating-point result.
\ does integer division and returns Integer/Long. This executes faster than
the above.

Reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vaoprintegerdivide.asp


Show quoteHide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1129923391.138103.217460@o13g2000cwo.googlegroups.com...
> The following function converts a decimal number into its binary
> equivalent (it gets fired when an OptionButton is checked):
>
> Function Dec2Bin(IntegerNumber As Long)
>    Dim iNum
>    Dim BinValue, TempValue
>    iNum = IntegerNumber
>    Do Until iNum = 0
>        TempValue = iNum Mod 2
>        BinValue = CStr(TempValue) + BinValue
>        MsgBox (iNum)
>        iNum = iNum \ 2
>    Loop
>
>    txtOutput.Text = BinValue
> End Function
>
> As such, the above function does the needful but there's one thing
> which I couldn't follow. Suppose I enter the decimal number 10 & then
> check the OptionButton that fires the above function. When I do so,
> iNum becomes equal to 10 & the message box first shows 10.....that's
> OK, then the message box shows 5....that's OK as well but after this,
> the message box shows 2! This is what I couldn't understand. How come
> the message box is showing 2? Shouldn't it be 2.5 instead because when
> the variable iNum is 5, then the line just before the Loop line divides
> 5 by 2 which is 2.5; so why is the message box showing 2 instead of
> 2.5? Can someone please explain me this?
>
> Thanks,
>
> Regards,
>
> Arpan
>
Author
22 Oct 2005 12:22 AM
Arpan
Thanks Someone for your input.

Regards,

Arpan
Author
22 Oct 2005 4:10 AM
Rick Rothstein [MVP - Visual Basic]
> Thanks Someone for your input.

Just to add a little more detail to the explanation of the "\"
operator...

     A \ B

is equivalent to this...

     Int(Round(A) / Round(B))

I mention this because there is a tendency to assume the
equivalence is this instead...

     Int(A / B)

and that is simply not the case. Anyway, considering the correct
equivalence above, if your A and B values are Integers or Longs,
the Round parts are immaterial. However, if either or both are
floating point values, then Round is important. For example,

     12.5 \ 3.5

will evaluate to 3 because the Round functions (in the
equivalences) use Banker's Rounding as its method of rounding (2.5
rounds down to 12 but 3.5 rounds up to 4).

Rick