C++
#include <iostream> using namespace std; int add( int a, int b) { for ( int i=1;i<=b;i++) //for loop will start from 1 and move till the value of second // number , first number(a) is incremented in for loop { a++; } return a; } int main(){ int a=add(10,32); // first number is 10 and second number is 32 , for loop will start cout<<a; // from 1 and move till 32 and the value of a is incremented 32 times // which will give us the total sum of two numbers return 0; } // This code is contributed by sayamsobti |
JAVA
import java.util.*; class GFG { static int add( int a, int b) { for ( int i = 1 ; i <= b; i++) // for loop will start from 1 and move till the value of second // number , first number(a) is incremented in for loop { a++; } return a; } public static void main(String[] args) { int a = add( 10 , 32 ); // first number is 10 and second number is 32 , for loop will start System.out.print(a); // from 1 and move till 32 and the value of a is incremented 32 times // which will give us the total sum of two numbers } } // This code contributed by Rajput-Ji |
Python3
# Python implementation def add(a, b): for i in range ( 1 , b + 1 ): #for loop will start from 1 and move till the value of second a = a + 1 # number , first number(a) is incremented in for loop a++; return a # driver code a = add( 10 , 32 ) # first number is 10 and second number is 32 , for loop will start print (a) # from 1 and move till 32 and the value of a is incremented 32 times # which will give us the total sum of two numbers # This code is contributed by shinjanpatra |
C#
using System; public class GFG { static int add( int a, int b) { for ( int i = 1; i <= b; i++) // for loop will start from 1 and move till the value of second // number , first number(a) is incremented in for loop { a++; } return a; } public static void Main(String[] args) { int a = add(10, 32); // first number is 10 and second number is 32 , for loop will start Console.Write(a); // from 1 and move till 32 and the value of a is incremented 32 times // which will give us the total sum of two numbers } } // This code is contributed by Rajput-Ji |
Javascript
<script> function add(a , b) { // for loop will start from 1 and move till the value of second // number , first number(a) is incremented in for loop for (i = 1; i <= b; i++) { a++; } return a; } // first number is 10 and second number is 32 , for loop will start var a = add(10, 32); // from 1 and move till 32 and the value of a is incremented 32 times // which will give us the total sum of two numbers document.write(a); // This code is contributed by Rajput-Ji </script> |
编写一个函数Add(),返回两个整数之和。该函数不应使用任何算术运算符(+、++、–、…等)。 两位之和可以通过对两位执行异或(^)来获得。进位可以通过执行两位的AND(&)来获得。
以上内容很简单 半加法器 可用于添加2个单个位的逻辑。我们可以将这种逻辑扩展为整数。如果x和y在相同的位置没有设置位,则x和y的按位异或(^)给出x和y的和。为了也合并公共设置位,使用按位和(&)。x和y的按位AND给出所有进位。我们计算(x&y)<<1,并将其加到x^y中,得到所需的结果。
C
// C++ Program to add two numbers // without using arithmetic operator #include <bits/stdc++.h> using namespace std; int Add( int x, int y) { // Iterate till there is no carry while (y != 0) { // carry should be unsigned to // deal with -ve numbers // carry now contains common //set bits of x and y unsigned carry = x & y; // Sum of bits of x and y where at //least one of the bits is not set x = x ^ y; // Carry is shifted by one so that adding // it to x gives the required sum y = carry << 1; } return x; } // Driver code int main() { cout << Add(15, 32); return 0; } // This code is contributed by rathbhupendra |
C
// C Program to add two numbers // without using arithmetic operator #include<stdio.h> int Add( int x, int y) { // Iterate till there is no carry while (y != 0) { // carry now contains common //set bits of x and y unsigned carry = x & y; // Sum of bits of x and y where at //least one of the bits is not set x = x ^ y; // Carry is shifted by one so that adding // it to x gives the required sum y = carry << 1; } return x; } int main() { printf ( "%d" , Add(15, 32)); return 0; } |
JAVA
// Java Program to add two numbers // without using arithmetic operator import java.io.*; class GFG { static int Add( int x, int y) { // Iterate till there is no carry while (y != 0 ) { // carry now contains common // set bits of x and y int carry = x & y; // Sum of bits of x and // y where at least one // of the bits is not set x = x ^ y; // Carry is shifted by // one so that adding it // to x gives the required sum y = carry << 1 ; } return x; } // Driver code public static void main(String arg[]) { System.out.println(Add( 15 , 32 )); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 Program to add two numbers # without using arithmetic operator def Add(x, y): # Iterate till there is no carry while (y ! = 0 ): # carry now contains common # set bits of x and y carry = x & y # Sum of bits of x and y where at # least one of the bits is not set x = x ^ y # Carry is shifted by one so that # adding it to x gives the required sum y = carry << 1 return x print (Add( 15 , 32 )) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# Program to add two numbers // without using arithmetic operator using System; class GFG { static int Add( int x, int y) { // Iterate till there is no carry while (y != 0) { // carry now contains common // set bits of x and y int carry = x & y; // Sum of bits of x and // y where at least one // of the bits is not set x = x ^ y; // Carry is shifted by // one so that adding it // to x gives the required sum y = carry << 1; } return x; } // Driver code public static void Main() { Console.WriteLine(Add(15, 32)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to add two numbers // without using arithmetic operator function Add( $x , $y ) { // Iterate till there is // no carry while ( $y != 0) { // carry now contains common //set bits of x and y $carry = $x & $y ; // Sum of bits of x and y where at //least one of the bits is not set $x = $x ^ $y ; // Carry is shifted by one // so that adding it to x // gives the required sum $y = $carry << 1; } return $x ; } // Driver Code echo Add(15, 32); // This code is contributed by anuj_67. ?> |
Javascript
<script> // Javascript Program to add two numbers // without using arithmetic operator function Add(x, y) { // Iterate till there is no carry while (y != 0) { // carry now contains common //set bits of x and y let carry = x & y; // Sum of bits of x and y where at //least one of the bits is not set x = x ^ y; // Carry is shifted by one so that adding // it to x gives the required sum y = carry << 1; } return x; } //driver code document.write(Add(15, 32)); // This code is contributed by Surbhi Tyagi </script> |
输出:
47
时间复杂性: O(对数y)
辅助空间: O(1)
下面是相同方法的递归实现。
C++
int Add( int x, int y) { if (y == 0) return x; else return Add( x ^ y,(unsigned) (x & y) << 1); } // This code is contributed by shubhamsingh10 |
C
int Add( int x, int y) { if (y == 0) return x; else return Add( x ^ y, (unsigned)(x & y) << 1); } |
JAVA
static int Add( int x, int y) { if (y == 0 ) return x; else return Add(x ^ y, (x & y) << 1 ); } // This code is contributed by subham348 |
Python3
def Add(x, y): if (y = = 0 ): return x else return Add( x ^ y, (x & y) << 1 ) # This code is contributed by subhammahato348 |
C#
static int Add( int x, int y) { if (y == 0) return x; else return Add(x ^ y, (x & y) << 1); } // This code is contributed by subhammahato348 |
Javascript
function Add(x, y) { if (y == 0) return x; else return Add(x ^ y, (x & y) << 1); } // This code is contributed by Ankita saini |
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END