Table of Contents
Flow chart
Example
Output
Conclusion
Home Backend Development C++ Implementing unsigned integer recovery division algorithm in C++

Implementing unsigned integer recovery division algorithm in C++

Sep 12, 2023 pm 04:01 PM
unsigned integer c language implementation Restoring the division algorithm

Discusses dividing unsigned integers using division algorithms. Some division algorithms are implemented on paper, others are implemented on digital circuits. There are two division algorithms: slow division algorithm and fast division algorithm. Slow division algorithms include recovery algorithms, non-execution recovery algorithms, SRT and non-recovery algorithms.

In this tutorial, we will discuss the recovery algorithm assuming 0 Solution method

Here, we will use register Q to store the quotient, register A to store the remainder, and M to store the divisor. The initial value of A remains at 0, and its value is restored, which is why the method resumes division.

  • Initialize register with value,

    • Initialize register with value, p>

      • Q = Dividend,

      • A = 0,

      • M = divisor,
      • N = number of dividend digits.

    • #Shifting AQ left means treating registers A and Q as one unit.

    • A is subtracted from M and stored in A.

    • Check the most significant bit of A:

      • If it is 0, set the least significant bit to 1.

      • Otherwise, set the least significant bit to 0.

    • #Restore the value of A and decrement the value of counter N.

    • If N = 0, break the loop; otherwise, go to step 2.

    • The quotient is stored in register Q.

    Flow chart

    Implementing unsigned integer recovery division algorithm in C++

    Example

    C code for the above method

    #include <iostream>
    using namespace std;
    int main(){
       // initializing all the variables with Dividend = 9, Divisor = 2.
       int Q = 8,q=1,M=3;
       short N = 4;
       int A = Q;
       M <<= N;
       // loop for division by bit operation.
       for(int i=N-1; i>=0; i--) {
          A = (A << 1)- M;
          // checking MSB of A.
          if(A < 0) {
             q &= ~(1 << i);  // set i-th bit to 0
             A = A + M;
          } else {
             q |= 1 << i;     // set i-th bit to 1
          }
       }
       cout << "Quotient: "<< q;
       return 0;
    }
    Copy after login

    Output

    Quotient: 2
    Copy after login

    Conclusion

    In this tutorial, we discussed the recovery division algorithm for unsigned integers. We discussed a simple way to solve this problem with the help of flowcharts and applying bit operations. We also discussed a C program to solve this problem and we can implement it using programming languages ​​like C, Java, Python etc. We hope you found this tutorial helpful.

The above is the detailed content of Implementing unsigned integer recovery division algorithm in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

See all articles