Quotient and remainder dividing by 2^k (a power of 2)

Given a positive integer n as a dividend and another number m (a form of 2^k), find the quotient and remainder without performing actual division

Examples:

Input : n = 43, m = 8
Output : Quotient = 5, Remainder = 3

Input : n = 58, m = 16
Output : Quotient = 3, Remainder = 10

An approach using bitwise operation

In this, we are using a bitwise representation of a number for understanding the role of division of any number by divisor of form 2^k. All numbers which are power of two include only 1 set bits in their representation and we will use this property. 

For finding the remainder we will take logical AND of the dividend (n) and divisor minus 1 (m-1), this will give only the set bits of dividend right to the set bit of divisor which is our actual remainder in that case. 

Further, the left part of the dividend (from the position of set bit in divisor) would be considered for quotient. So, from dividend (n) removing all bits right from the position of set bit of divisor will result in the quotient, and right shifting the dividend log2(m) times will do this job for finding the quotient
 

  • Remainder = n & (m-1)
  • Quotient = (n >> log2(m) )


Note: Log2(m) will give the number of bits present in the binary representation of m.
 

Finding Quotient and Remainder

Below is the implementation:

C++
#include <iostream>
using namespace std;

// function to print remainder and quotient
void divide(int n, int m) {
    if (m == 0) {
        cout << "Error: Division by zero is not allowed." << endl;
        return;
    }

    int remainder = n % m;
    int quotient = n / m;

    cout << "Remainder = " << remainder << endl;
    cout << "Quotient = " << quotient << endl;
}

// driver program
int main() {
    int n = 43, m = 8;
    divide(n, m);
    return 0;
}
Java
import java.util.Scanner;

public class Main {
    // Function to print remainder and quotient
    public static void divide(int n, int m) {
        if (m == 0) {
            System.out.println("Error: Division by zero is not allowed.");
            return;
        }

        int remainder = n % m;
        int quotient = n / m;

        System.out.println("Remainder = " + remainder);
        System.out.println("Quotient = " + quotient);
    }

    // Main method
    public static void main(String[] args) {
        int n = 43, m = 8; // Example input
        divide(n, m);
    }
}
Python
def divide(n, m):
    if m == 0:
        print("Error: Division by zero is not allowed.")
        return

    remainder = n % m
    quotient = n // m

    print("Remainder =", remainder)
    print("Quotient =", quotient)

# Main
if __name__ == "__main__":
    n, m = 43, 8  # Example input
    divide(n, m)
C#
using System;

class MainClass {
    // Function to print remainder and quotient
    public static void Divide(int n, int m) {
        if (m == 0) {
            Console.WriteLine("Error: Division by zero is not allowed.");
            return;
        }

        int remainder = n % m;
        int quotient = n / m;

        Console.WriteLine("Remainder = " + remainder);
        Console.WriteLine("Quotient = " + quotient);
    }

    // Main method
    public static void Main (string[] args) {
        int n = 43, m = 8; // Example input
        Divide(n, m);
    }
}
Javascript
// Function to print remainder and quotient
function divide(n, m) {
    if (m === 0) {
        console.log("Error: Division by zero is not allowed.");
        return;
    }

    let remainder = n % m;
    let quotient = Math.floor(n / m);

    console.log("Remainder =", remainder);
    console.log("Quotient =", quotient);
}

// Main
let n = 43, m = 8; // Example input
divide(n, m);
PHP
<?php
// Function to print remainder and quotient
function divide($n, $m) {
    if ($m == 0) {
        echo "Error: Division by zero is not allowed.";
        return;
    }

    $remainder = $n % $m;
    $quotient = (int)($n / $m);

    echo "Remainder = " . $remainder . "\n";
    echo "Quotient = " . $quotient . "\n";
}

// Main
$n = 43; $m = 8; // Example input
divide($n, $m);
?>

Output
Remainder = 3
Quotient = 5

Time Complexity: O(1)
Auxiliary Space: O(1)
 



Contact Us