Summe zweier Zahlen mit bitweisem Operator

Ich füge den Code ein, um die Summe von zwei Zahlen mit dem bitweisen Operator zu finden. Bitte schlagen Sie vor, ob es optimiert werden kann. Vielen Dank...

public static int getSum(int p, int q)
{
int carry=0, result =0;
for(int i=0; i<32; i++)
{
    int n1 = (p & (1<<(i)))>>(i); //find the nth bit of p
    int n2 = (q & (1<<(i)))>>(i); //find the nth bit of q

    int s = n1 ^ n2 ^ carry; //sum of bits
    carry = (carry==0) ? (n1&n2): (n1 | n2); //calculate the carry for next step
    result = result | (s<<(i)); //calculate resultant bit
}

return result;
}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage