Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Maths Involved

Thread view: 
Calum Maciver - 18 Feb 2006 00:51 GMT
I've got an example program that converts positive integers into
binary. I'm having trouble understanding how the maths involved works.
Normally you work out the binary back to front but here it does it the
otherway round. Could someone please explain why this works.

public void printBinary( int n )
    {
        int v = 1;

        while (v <= n/2)
            v *= 2;

        while ( v > 0 )
        {
            if  (n < v)
                System.out.print("0");
            else
            {
                System.out.print("1");
                n = n - v;
            }
            v = v/2;
        }
    }
James McGill - 18 Feb 2006 01:26 GMT
> Could someone please explain why this works.

Integer "divide" and "multiply" by 2, are just a rich man's right and
left shift, respectively.
tom fredriksen - 19 Feb 2006 18:25 GMT
>> Could someone please explain why this works.
>
> Integer "divide" and "multiply" by 2, are just a rich man's right and
> left shift, respectively.

That what I was thinking as well! Why in gods name do it so complex.

Here is an example of how it could be done in a legible way, not to
mention within the normal idiom of such tasks

    int marker = 128;
    for(int c=0; c<8; c++) {
        if( (marker & value) == marker ) {
            System.out.print("1");
        } else {
            System.out.print("0");
        }
        marker = marker >>> 1;
    }

/tom
Luke Webber - 19 Feb 2006 22:17 GMT
>> Could someone please explain why this works.
>
> Integer "divide" and "multiply" by 2, are just a rich man's right and
> left shift, respectively.

Hell yes. Even in languages that lack shift operators, it would be
smarter to use v += v than v *= 2. Hopefully the optimiser works that
out, though.

Luke
Michael Redlich - 18 Feb 2006 01:31 GMT
> I've got an example program that converts positive integers into
> binary. I'm having trouble understanding how the maths involved works.
> Normally you work out the binary back to front but here it does it the
> otherway round. Could someone please explain why this works.

Hi Calum:

I assume that back-to-front is right-to-left, yes?

I was just working out your algorithm on paper using n = 10.  The
different values of the variable, v, in the while loop are as follows:

8, 4, 2, 1, 0

which correspond to:

2^3, 2^2, 2^1, 2^0

So I assume that the variable, v, represents the exponents for base 2.
If that's the case, then your program does work as intended, it's just
doing it from left-to-right instead of right-to-left.

Whaddya think...make sense?

Mike.

-----
ACGNJ Java Users Group
http://www.javasig.org/
Roedy Green - 18 Feb 2006 02:47 GMT
On 17 Feb 2006 16:51:28 -0800, "Calum Maciver"
<calummaciver@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>I've got an example program that converts positive integers into
>binary. I'm having trouble understanding how the maths involved works.
>Normally you work out the binary back to front but here it does it the
>otherway round. Could someone please explain why this works.

get a piece of paper and pretend to be a computer. for each variable
reserve some space on your paper to record it latest value.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.