-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solve divide two integers problem using bit manipulation
- Loading branch information
1 parent
a99d1cb
commit 9dfe1b3
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <iostream> | ||
#include <climits> | ||
|
||
using namespace std; | ||
|
||
int divide(int dividend, int divisor) | ||
{ | ||
if (dividend == divisor) | ||
{ | ||
return 1; | ||
} | ||
|
||
bool isPositive = dividend > 0 == divisor > 0; // true both dividend and divisor have the same sign | ||
unsigned int divd = abs(dividend); // short for dividend since this name is already used | ||
unsigned int divs = abs(divisor); // short for divisor since this name is already used | ||
unsigned int ans = 0; | ||
|
||
// dividend = quotient * divisor + remain | ||
while (divd >= divs) | ||
{ | ||
int exp = 0; | ||
|
||
/* | ||
* "<<" left-shift bitwise operator. a << b moves the bits in "a" by "b" | ||
* positions. Given as a result: a * 2^b | ||
* | ||
* This loop will increment the exp until the result of: | ||
* divisor * 2^exp | ||
* is greater than the dividend. | ||
*/ | ||
while ((divs << (exp + 1)) < divd) | ||
{ | ||
exp++; | ||
} | ||
|
||
/* | ||
* This will end up in adding all the powers of 2 that compose the quotient | ||
* i.e. getting the value of quotient by a binary representation. | ||
*/ | ||
ans += (1 << exp); // ans += 2^exp | ||
divd = divd - (divs << exp); | ||
} | ||
|
||
if (ans == (1 << 31) && isPositive) | ||
{ | ||
return INT_MAX; | ||
} | ||
|
||
return isPositive ? ans : -ans; | ||
} | ||
|
||
int main() | ||
{ | ||
int dividend; | ||
cin >> dividend; | ||
|
||
int divisor; | ||
cin >> divisor; | ||
|
||
cout << "Result: " << divide(dividend, divisor) << endl; | ||
|
||
return 0; | ||
} |