c 语言位操作
今天在看《Unix 环境高级编程》时对信号集的操作(P254),这个在对寄存器或者标志位做位操作时很有借鉴意义。
#BIT_WIDTH 32 #define BITBAD( bit ) ( (bit)<=0 || (bit) >= BIT_WIDTH ) // set one bit in the flag bit_set( long flag, int bit ) { if( BITBAD(bit) ) oops(" bit is not correct"," "); *flag |= 1<<( bit - 1 ); } //delete one bit in the flag bit_del( long flag, int bit ) { if( BITBAD(bit) ) oops(" bit is not correct"," "); *flag &= ~( 1<<( bit - 1 ) ); } //test one bit int flag bit_test( long flag, int bit ) { if( BITBAD(bit) ) oops(" bit is not correct"," "); return( (*flag & ( 1<<( bit-1) ) ) != 0 ); }