Mit bitweise Operatoren können Sie zwei Zahlen hinzufügen. Versuchen Sie unter:
int Sum(int a, int b)
{
// Iterate till there is no carry
while (b != 0)
{
// now carry 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;
}
Mit erhöhen bzw. verringern Operatoren Sie zwei Zahlen addieren. Der andere Weg könnte wie sein:
int Sum(int a, int b)
{
// Iterate till there b becomes zero
while (b--)
{
a++;
}
return a;
}
Vielleicht 'x - (- y)'? – coredump
Voting zum Schließen einer C-Frage als Duplikat einer C# -Frage ist nicht korrekt. – 2501
@coredump tat das gleiche, aber er bestand darauf, dass ich das nicht tat !! – Anjaneyulu