← DiffPush

Divide two numbers using bit maipulation

Baseline Bit Manipulation · Learn Bit Manipulation O(log n) over the absolute dividend · O(1)

Subdividing the Stockroom Shelf Without a Calculator

A stockroom robot must split a shelf of length `dividend` into bays of length `divisor` — but its controller has no multiply, divide, or modulo circuit. It can only double and shift. The robot repeatedly finds the largest doubled divisor that still fits, peels that block off, and records how many such blocks each doubling step contributed, until the shelf is fully subdivided.

Input: Two integers dividend and divisor (divisor is never zero).

Output: The quotient, truncated toward zero.

Constraints

Examples

Example 1

Input: {"dividend":10,"divisor":3}
Output: 3
Three full bays of 3 fit with a remainder of 1.

Example 2

Input: {"dividend":43,"divisor":-8}
Output: -5
Opposite signs flip the quotient; 43 / 8 truncates to 5, so the answer is -5.

Solve this in your browser →

Also on LeetCode ↗