5.1 操作符
+ - * / %
除了%操作符,其余几个操作符都是既适用于浮点型又适用于整型。%要求必须是整型。
5.1.2 移位操作符
移位操作符必须是整型。右移位操作存在两种移位方式:逻辑右移和算术右移。
逻辑右移:用0来填充左边空出来的位
算术右移:用符号位来填充左边空出来的位。
下列代码返回参数值中1的位个数:
int count_one_bits( unsigned value ){ int ones; for ( ones = 0; value != 0; value = value >> 1 ){ if ( value % 2 != 0 ){ ones += 1; } } return ones;}5.1.3 位操作符
要求操作数为整型类型。
例子1:将指定的位设置为1
value = value | 1 << bit_number;例子2:将指定的位清0
value = value & ~(1 << bit_number);例子3:对指定的位进行测试
value & 1 << bit_number;5.1.4 赋值和复合赋值符
计算一个值中值为1的位的个数:
int count_one_bits( unsigned value ){ int ones = 0; for ( ones = 0; value != 0; value >>= 1 ){ if ( ( value & 1 ) != 0 ){ ones += 1; } } return ones;}
5.3 左值和右值
左值就是那些能够出现在赋值符号左边的东西。右值就是那些可以出现在赋值符号右边的东西。左值有个要求是:必须表示一个特定的位置。所以字符串常量不能是左值,因为它在内存中的存储位置是随机的。
习题:
1.
#include#include int main(void){ int ch; while ( ( ch = getchar() ) != EOF ){ if ( ( ch >= 'A' ) && ( ch <= 'Z' ) ){ putchar( ch - 'A' + 'a' ); continue; } putchar( ch ); } return 0;}
程序输入输出:
2.
#include#include int main(void){ int ch; while ( ( ch = getchar() ) != EOF ){ if ( ( ch >= 'a') && ( ch <= 'z' ) ){ putchar( (ch - 'a' >= 13 ) ? ch - 13 : ch + 13 ); continue; } else if ( ( ch >= 'A' ) && ( ch <= 'Z' ) ){ putchar( (ch - 'A' >= 13 ) ? ch - 13 : ch + 13 ); continue; } putchar( ch ); } return 0;}
程序输入输出:
3.
#include#include #include unsigned int reverse_bits( unsigned int value ){ unsigned int tempValue = 0; int i = 0; while ( i < sizeof( unsigned int ) * 8 - 1){ tempValue += ( value & ( 1 << i ) ? 1 : 0 ) << ( sizeof( unsigned int ) * 8 - i - 1 ); i++; } return tempValue;}int main(void){ printf("%x\n", reverse_bits(25)); return 0;}
我用十六进制看了后,答案是正确的,但是如果用%d来显示,却依旧是个负数,虽然已经是unsigned int了,不知道为什么。
4.
#include#include #define MAXLINE 128void set_bit( char bit_array[], unsigned bit_number ){ bit_array[bit_number] = 1;}void clear_bit( char bit_array[], unsigned bit_number ){ bit_array[bit_number] = 0;}void assign_bit( char bit_array[], unsigned bit_number, int value ){ if ( !value ){ bit_array[bit_number] = 0; } else{ bit_array[bit_number] = 1; }}int test_bit( char bit_array[], unsigned bit_number ){ return bit_array[bit_number];}int main(void){ char bit_array[MAXLINE]; int i = 0; for ( i = 0; i < MAXLINE; i++ ){ bit_array[i] = 0; } set_bit( bit_array, 5 ); printf("%d\n", bit_array[5] ); clear_bit( bit_array, 5 ); printf("%d\n", bit_array[5] ); assign_bit( bit_array, 5, 4 ); printf("%d\n", bit_array[5] ); printf("%d\n", test_bit( bit_array, 5 ) ); return 0;}
程序输入输出:
5.
#include#include int store_bit_field( int original_value, int value_to_store, unsigned starting_bit, unsigned ending_bit ){ int maskValue = 0; int maskValueForStore = 0; int new_value_to_store = 0; int i = 0; for ( i = ending_bit; i <= starting_bit; i++ ){ maskValue += 1 << i; maskValueForStore += 1 << (i - ending_bit); } original_value &= ~maskValue; value_to_store &= maskValueForStore; value_to_store <<= ending_bit; return original_value | value_to_store;}int main(void){ int value = store_bit_field( 0xffff, 0x123, 13, 9 ); printf("%x\n", value); return 0;}
程序输出: