Spyder_ab

Multiply And Add Two Numbers without any arithmetic operators

Oct 19th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll Add(ll a,ll b){
  5.  
  6. while(b){
  7. ll carry=a&b;
  8. a=a^b;
  9. b=carry<<1;
  10. }
  11. return a;
  12.  
  13. }
  14. ll Multiply(ll a,ll b){
  15. ll res=0;
  16. while(b){
  17. if(b&1){
  18. res=Add(res,a);
  19. }
  20. a<<=1;
  21. b>>=1;
  22. }
  23. return res;
  24. }
  25. int main(){
  26.  
  27. ll x,y;
  28. cin>>x>>y;
  29. cout<<Multiply(x,y);
  30.  
  31. return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment