C语言与C++的差异, 要害在于位运算和宏操纵, 可以通过位移(<<)和与运算(&), 把指定位置为0.
代码:
/* * test.cpp * * Created on: 2014.05.23 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #define BIT_MASK(bit_pos) (0x01 << (bit_pos)) using namespace std; int Bit_Reset(unsigned int* val, unsigned char pos) { if (pos >= sizeof(unsigned int)*8) { return 0; } *val = (*val & ~BIT_MASK(pos)); //与0做与(&)运算 return 1; } int main() { unsigned int x = 0xffffffff; unsigned char y = 4; Bit_Reset(&x, y); std::cout << std::hex << x << std::endl; return 0; }
输出:
ffffffef
From:csdn博客 Spike_King