This question already has an answer here:
这个问题在这里已有答案:
- What is the best way to add two numbers without using the + operator? 20 answers
不使用+运算符添加两个数字的最佳方法是什么? 20个答案
I am 9 grade, My math teacher asked me to add numbers with out using +
sign in C program.
我9年级,我的数学老师让我在C程序中使用+符号添加数字。
I tried a - (-b) = a + b;
but my math teacher want some other option.
我试过 - (-b)= a + b;但我的数学老师想要一些其他选择。
5 个解决方案
#1
7
Using Anti Log()
you can do that
使用Anti Log()可以做到这一点
Temp= Anti Log(a)* Anti Log(b);
a+b value is equals to log(Temp);
Works for integers not for double.
适用于不是双精度的整数。
#2
21
Use this function in your c program
在c程序中使用此功能
int Add(int a, int b)
{
while (b)
{
// carry now contains common set bits of "a" and "b"
int carry = a & b;
// Sum of bits of "a" and "b" where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to "a" gives the required sum
b = carry << 1;
}
return a;
}
#3
11
Use bitwise ^
and &
operators and recursion
使用按位^和&运算符和递归
int add(int x, int y){
return y == 0 ? x : add( x ^ y, (x & y) << 1);
}
P.S.: It is the recursive version of an algorithm proposed by vikas.
P.S。:它是vikas提出的算法的递归版本。
#4
6
In Java using recursion-
在Java中使用递归 -
public static int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
In C-
int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
#5
1
#include<stdio.h>
int add(int a, int b) {
return (int)&((char *)a)[b];
}
int main() {
printf("%d", add(5, 17));
getchar();
}
Not portable but doesn't use the "+" character. This casts a to a char pointer, adds b to it with [] and then casts it back to an int.
不可携带,但不使用“+”字符。这会将a转换为char指针,使用[]向其添加b,然后将其强制转换为int。
#1
7
Using Anti Log()
you can do that
使用Anti Log()可以做到这一点
Temp= Anti Log(a)* Anti Log(b);
a+b value is equals to log(Temp);
Works for integers not for double.
适用于不是双精度的整数。
#2
21
Use this function in your c program
在c程序中使用此功能
int Add(int a, int b)
{
while (b)
{
// carry now contains common set bits of "a" and "b"
int carry = a & b;
// Sum of bits of "a" and "b" where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to "a" gives the required sum
b = carry << 1;
}
return a;
}
#3
11
Use bitwise ^
and &
operators and recursion
使用按位^和&运算符和递归
int add(int x, int y){
return y == 0 ? x : add( x ^ y, (x & y) << 1);
}
P.S.: It is the recursive version of an algorithm proposed by vikas.
P.S。:它是vikas提出的算法的递归版本。
#4
6
In Java using recursion-
在Java中使用递归 -
public static int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
In C-
int add(int a, int b){
if(b == 0) return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
#5
1
#include<stdio.h>
int add(int a, int b) {
return (int)&((char *)a)[b];
}
int main() {
printf("%d", add(5, 17));
getchar();
}
Not portable but doesn't use the "+" character. This casts a to a char pointer, adds b to it with [] and then casts it back to an int.
不可携带,但不使用“+”字符。这会将a转换为char指针,使用[]向其添加b,然后将其强制转换为int。