如何使用按位添加位?
我试图找出如何仅使用以下按位运算来添加位(最多2个字节):〜&^ | <>。 我一直试着没有运气。 我想知道是否有人知道如何。
int logicalByteAdd(int x, int y) { return ; }
unsigned short add(unsigned short a, unsigned short b) { unsigned short carry = a & b; unsigned short result = a ^ b; while(carry != 0) { unsigned short shiftedcarry = carry << 1; carry = result & shiftedcarry; result ^= shiftedcarry; } return result; }
Mooing Duck提供的正确性certificate