C语言中位运算置0操作
          
            时间:12-05 13:42 阅读:1034次
*温馨提示:点击图片可以放大观看高清大图
                
         
        
		  
               简介: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