《C和指针》学习笔记[第五章 操作符与表达式]
5.8问题
1.
2.0
2.
答案不唯一,我的机器输出为9
3.
移位操作符与位操作符本人操作比较少,我就知道子网掩码与ip做位运算,可以知道是否同一个子网
4.
两个速度一样
5.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" int main(void) { int leap_year; int year; scanf("%d", &year); leap_year = (year % 4 ==0 && year % 100 !=0) || year % 400 == 0 ? 1 : 0; printf("%d\n", leap_year); return EXIT_SUCCESS; }
6.
该题目我摘抄的信息 C 语言中,术语副作用是指对数据对象或者文件的修改。
任何一个操作符本身应该有返回值,所谓的副作用用就是改变了对象。
所以=, ++,--,+=等复合操作符,都对变量本身进行了操作,比如a=1,该表达式的返回值为a,但副作用是把a的内容修改了,另外的同理。
7.
显示 In range,因为 1<=a为1,1<=10为1,所以显示if条件为真的情况
8.
整个代码,我感觉已经比较简洁了,并没有消除多余代码的必要
9.
我觉得能
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" #define ARRAY_SIZE 10 int main(void) { int non_zeroo = 0; int array[ARRAY_SIZE] = {0}; int i; for (i = 0; i < ARRAY_SIZE; i += 1) { non_zeroo += array[i]; } if (!non_zeroo) { printf("Value are all zero\n"); } else printf("There are non zero values\n"); return EXIT_SUCCESS; }
10.
a -25 b -25 c 9 d 1 e 4
f -2 [f -5] g 20[ g 40] h -3 [ h -4]i 1 j 0[j 10]
12.
本电脑为算数移位,因为-2右移一位变成了-1,如果为逻辑移位,最高位补0应该变成正数
5.9 编程练习
1.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" int main(void) { int ch; while ((ch = getchar()) != EOF) { if (ch >= 'A' && ch <= 'Z') { ch += 'a' - 'A'; } putchar(ch); } return EXIT_SUCCESS; }
2.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" int main(void) { int ch; while ((ch = getchar()) != EOF) { if (ch >= 'A' && ch <= 'Z'){ ch += 13; if (ch > 'Z') { ch = ch % 'Z' + 'A'; } } else if (ch >= 'a' && ch <= 'z'){ ch += 13; if (ch > 'z') { ch = ch % 'z' + 'a'; } } putchar(ch); } return EXIT_SUCCESS; }
3.[摘抄来至网上的答案,确实是非常好的思路]
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include "t_c.h" unsigned int reverse( unsigned int value ) { unsigned int answer = 0; unsigned int i; /* 用i来控制循环的次数,利用位操作,增加程序的可移植性, 用1的常数来判断移尾确实思路很好 for( i = 1; i != 0; i <<= 1) */ for ( i = 1; i != 0 ; i <<= 1) { answer <<= 1; if( value & 1 ) answer |= 1; value >>= 1; } return answer; } int main(void) { printf("%u\n", reverse(25) ); return EXIT_SUCCESS; }
4.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include "t_c.h" void set_bit(char bit_array[], unsigned bit_number) { signed char ch, o_ch = 1, i; int index, offset; index = (bit_number-1) / 8; offset = bit_number % 8; // printf("%d, %d \n", index, offset); ch = bit_array[index]; for (i = 0; i < 8 - offset; i++) { o_ch <<= 1; } ch |= o_ch; bit_array[index] = ch; } void clear_bit(char bit_array[], unsigned bit_number) { signed char ch, o_ch = 0, i; int index, offset; index = (bit_number-1) / 8; offset = bit_number % 8; ch = bit_array[index]; for (i = 0; i < 8; i++) { if (i == offset) { o_ch |= 0; } else o_ch |= 1; o_ch <<= 1; } ch &= o_ch; bit_array[index] = ch; } void assign_bit(char bit_array[], unsigned bit_number, int value) { // 调用前面定义好的函数 if (value) { set_bit(bit_array, bit_number); } else clear_bit(bit_array, bit_number); } int test_bit(char bit_array[], unsigned bit_number) { signed char ch, o_ch = 0, i; int index, offset; index = (bit_number-1) / 8; offset = bit_number % 8; ch = bit_array[index]; for (i = 0; i < 8; i++) { if (i == offset) { o_ch |= 1; } else o_ch |= 0; o_ch <<= 1; } ch &= o_ch; ch = ch ? 1: 0; return (int) ch; } int main(void) { int c; char string[] = "Hello"; c = test_bit(string, 2); printf("%d\n", c); return EXIT_SUCCESS; }
上面我写的,好像对题目理解错误了,尴尬。这是人家正确的理解:
#include<stdio.h> #include<string.h> void set_bit( char bit_array[], unsigned int bit_number ) { int length = strlen( bit_array ); if( bit_number < length ) { bit_array[length - bit_number - 1] = '1'; } } void clear_bit( char bit_array[], unsigned int bit_number ) { int length = strlen( bit_array ); if( bit_number < length ) { bit_array[length - bit_number - 1] = '0'; } } void assign_bit( char bit_array[], unsigned int bit_number, int value ) { int length = strlen( bit_array ); if ( bit_number < length ) { if ( value == 0) { bit_array[length - bit_number - 1] = '0'; } else if( value == 1) { bit_array[length - bit_number - 1] = '1'; } } } int test_bit( char bit_array[], unsigned bit_number ) { int length = strlen( bit_array ); if ( bit_number < length ) { if ( bit_array[bit_number] != '0') { return 1; } else { return 0; } } } int main(int argc, char const *argv[]) { char str[] = "0000"; set_bit( str, 1 ); printf("%s\n", str ); return 0; }
应该是通过字符串模拟一串二进制数据,我想复杂了
5.摘操至:https://github.com/Stephan14/Pointers_On_C/blob/master/ch5/p5.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define INI_BITS ( CHAR_BIT * sizeof(int) ) /* 已经经过测试该编译器中规定int为4个字节 */ int creat_mask( int starting_bit, int ending_bit ) { /*为保证进行逻辑移位而不是算术移位,将掩码声明为无符号类型 * 通过有符号的-1,二进制全1,左右移位,形成掩码确实思路不错,书上给了提示。我还是没想到 */ unsigned int mask = (unsigned)-1;//强制类型转换 mask >>= INI_BITS - (starting_bit - ending_bit + 1 ); mask <<= ending_bit; return mask; } // 这里就比较简单,按照书中逻辑操作 int store_bit_field( int original_value, int value_to_store, unsigned starting_bit, unsigned ending_bit ) { unsigned int mask = creat_mask( starting_bit, ending_bit ); original_value &= ~mask; value_to_store <<= ending_bit; return original_value | (value_to_store & mask); } int main(void) { int i = 0xffff; printf("%x\n", store_bit_field( i, 0x123, 13, 9) ); return 0; }
本章学习对于位运算,我还是太陌生了,而且对于有符号数,与无符号数的认知也比较少。