我想設置一個給定位從一個位掩碼到另一個位於C中。這是我目前的做法。如何在C中將一個位掩碼設置爲另一位?
const int MASK_THIRD = (1<<2);
if (mask & MASK_THIRD) {
mask_another |= MASK_THIRD;
} else {
mask_another &= ~MASK_THIRD;
}
有沒有更智能的方法呢?在mask_another (mask_another & (~MASK_THIRD))
我同意這一點,但它更難理解:) – marbel82
同意;雖然XOR很有趣! –