Results 1 to 10 of 10

Thread: How do I align numbers in ANSI C?

  1. #1
    Join Date
    Mar 2007
    Beans
    1,052

    How do I align numbers in ANSI C?

    I'm attempting to code the Binary Division by Shift and Subtract algorithm ( http://courses.cs.vt.edu/~cs1104/Div....Subtract.html ) but, I do not understand how to “align [the] leftmost digits in [the] dividend and [the] divisor”.

    If more information is needed/wanted, tell me and, I will give it to you.

    Any help in figuring this out would be greatly appreciated!
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: How do I align numbers in ANSI C?

    In which language?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Mar 2007
    Beans
    1,052

    Re: How do I align numbers in ANSI C?

    ANSI C (as the title suggests).
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: How do I align numbers in ANSI C?

    Sorry, I forgot the title after I read the link.

    One way to do this would be to find out the position of the leading 1-bit in both numbers (Google will give you some algorithms to do that), compute the difference between them and switch the one whose leftmost 1-bit is furthest to the right by that amount.
    Last edited by Bachstelze; January 27th, 2013 at 07:34 PM.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How do I align numbers in ANSI C?

    Quote Originally Posted by bachstelze View Post
    and switch the one whose leftmost bid
    iyswim

  6. #6
    Join Date
    Mar 2007
    Beans
    1,052

    Re: How do I align numbers in ANSI C?

    Sorry for the dumb question(s) but, I am very confused.

    Is this comparison supposed to happen in a data structure or with a print statement or what?

    Could you give me a mini-example please?
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How do I align numbers in ANSI C?

    Well if you're actually implementing the division then you would need to do it on the variables that are holding the values

    You should probably do some reading up on arithmetic shifts

    http://en.wikipedia.org/wiki/Arithmetic_shift

    In C, the operator you're going to need is the bitwise left shift '<<' e.g.

    Code:
      unsigned int divisor = 3;
      .
      .
      .
        divisor = divisor << n;

  8. #8
    Join Date
    Mar 2007
    Beans
    1,052

    Re: How do I align numbers in ANSI C?

    Is the following well done?:
    Code:
    // Shift and Subtract Algorithm
    quotient = 0;
    
    	while(dividend < divisor)
    	{
    		if(dividend >= divisor)
    		{
    			dividend = dividend - divisor;
    			quotient = quotient << 1;
    		}
    		else
    		{
    			quotient = quotient << 0;
    		}
    		
    		divisor = divisor >> 1;
    	}
    }
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  9. #9
    Join Date
    Mar 2007
    Beans
    1,052

    Re: How do I align numbers in ANSI C?

    Wait, something is wrong since the inner if statement will never get executed since it is the negation of the while loop's boolean statement.

    Could someone please help me out? I'm trying to implement the algorithm here ( http://courses.cs.vt.edu/~cs1104/Div....Subtract.html ).
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  10. #10
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: How do I align numbers in ANSI C?

    Quote Originally Posted by s3a View Post
    Is the following well done?:
    Maybe. Does it give correct results?
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •