w3resource

How to divide numbers in C without using *, /, +, -, or % operators

Divide numbers without *, /, +, -, or % in C

In C, dividing one number by another without using basic arithmetic operators like *, /, +, -, or % can be challenging. We can perform this operation using bitwise operators and a loop to achieve the division result. Bitwise operators are often used for such tasks because they operate directly on the binary representation of numbers, making them efficient for tasks like shifting and masking.

Approach:

Bitwise Shifts: We use left-shift (<<) and right-shift (>>) operations to represent multiplication and division by powers of 2.

Subtraction Using Bitwise Operations: Since direct subtraction is restricted, we use repeated shifting and comparison to achieve the division.

Example Code:

This program divides an integer dividend by another integer divisor using only bitwise operations and loops. It calculates the quotient by shifting the divisor left until it surpasses the dividend, then subtracts it in a way that simulates division.

Code:

#include <stdio.h> 
int divide(int dividend, int divisor) {
    // Check for division by zero
    if (divisor == 0) {
        printf("Error: Division by zero is undefined.\n");
        return -1;  // Returning -1 to indicate an error
    }
    
    // Handle negative numbers and track the result sign
    int sign = 1;
    if ((dividend < 0) ^ (divisor < 0)) {
        sign = -1;
    }

    // Make dividend and divisor positive
    unsigned int absDividend = (dividend < 0) ? -dividend : dividend;
    unsigned int absDivisor = (divisor < 0) ? -divisor : divisor;

    int quotient = 0;

    // Left shift the divisor until it is larger than dividend
    while (absDividend >= absDivisor) {
        int count = 0;
        // Shift divisor until it surpasses dividend
        while (absDividend >= (absDivisor << count)) {
            count++;
        }
        // Subtract the shifted value from dividend
        quotient += (1 << (count - 1));
        absDividend -= (absDivisor << (count - 1));
    }

    return sign * quotient;  // Apply the correct sign to the result
}

int main() {
    int dividend = 25;
    int divisor = 7;

    int result = divide(dividend, divisor);

    if (result != -1) {
        printf("Quotient of %d / %d = %d\n", dividend, divisor, result);
    }

    return 0;
}

Output:

Quotient of 25 / 7 = 3

Explanation:

  • Sign Tracking: We use the XOR operator (^) to determine if the result should be negative. If either dividend or divisor is negative, but not both, the result is negative.
  • Absolute Values: We convert both dividend and divisor to their absolute values to simplify the division.
  • Bitwise Left Shift: For each possible power of two, we check if the shifted divisor fits within the dividend.
  • Subtraction via Shifting: We simulate the subtraction by shifting and updating quotient iteratively.

Summary:

The code divides two integers using bitwise shifts to simulate division.

It avoids arithmetic operators by using repeated shifts and comparisons.

The approach efficiently handles both positive and negative numbers.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/c-snippets/divide-a-number-by-another-number-without-using-arithmetic-operators.php