Skip to content

Commit d7c2b53

Browse files
committed
Added divide_integers in Bit Manipulation
1 parent 80ee47d commit d7c2b53

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Divide two integers without using multiplication, division and mod operator. Print floor of division
2+
3+
#include<iostream>
4+
5+
using namespace std;
6+
7+
int divide(int A, int B) {
8+
long long int ans=0,sign;
9+
if(B==1||B==-1){
10+
if(B==1)
11+
return min(INT_MAX,A);
12+
else{
13+
long long int x=(long long int)A*(-1);
14+
return min((long long int)INT_MAX,x);
15+
}
16+
}
17+
sign=((A<0)^(B<0))?-1:1;
18+
A=abs(A);
19+
B=abs(B);
20+
while(A>=B){
21+
A-=B;
22+
ans++;
23+
}
24+
return (int)(ans*sign);
25+
}
26+
27+
int main()
28+
{
29+
int a,b,x;
30+
scanf("%d %d",&a,&b);
31+
printf("Division is %d\n",divide(a,b));
32+
return 0;
33+
}

0 commit comments

Comments
 (0)