保留哪一位,哪一位 & 1; 147 // left2 = left2 & 0b10101010; 148 // //偶数位移动到奇数位 149 // unsigned char right2 = a2 >> 1; 150 // //保留奇数位,将偶数位清0 151 // right2 = right2 & 0b01010101; 152 //
153 // a2 = left2 | right2; 154 // printf("%d\n", a2); 155 //
156
157 // //3. 循环移位。 158 // int num = 0; 159 // printf("请输入左移的位数:"); 160 // scanf("%d",&num); 161 // num %= 8; 162 // unsigned char a3 = 0b11111111; 163 // //左移 num 位 164 // unsigned char left3 = a3 << num; 165 // //右移 8 - num 位 166 // unsigned char right3 = a3 >> (8 - num); 167 //
168 // a3 = left3 | right3; 169 // printf("%d\n", a3); 170 //
171 //
172
173
174
175 //四、宏的使用 176 //************************* 177
178 // int a[N] = {0}; 179 // for (int i = 0; i < N; i++) { 180 // a[i] = arc4random() % (20 - 10 + 1) + 10; 181 // printf("%d ", a[i]); 182 // } 183 // for (int i = 0 ; i < N - 1; i++) { 184 // for (int j = 0; j < N - 1 - i; j++) { 185 // if (a[j] > a[j + 1]) { 186 // int temp = a[j]; 187 // a[j] = a[j + 1]; 188 // a[j + 1] = temp; 189 // } 190 // } 191 // } 192 // printf("\n"); 193 // for (int i = 0; i < N; i++) { 194 // printf("%d ", a[i]); 195 // } 196 //
197
198 //printf("%d", MUL(10 + 5, 5 + 10)); 199
200
201
202
203 //五、条件编译 204 //************************* 205 //根据不同的条件,编译不同的代码 206 //1.编辑(写代码) 2.预编译(替换) 3.编译(生成目标文件) 4.链接 5.运行 207
208 //第一种形式 209 //#define ASHEN 210 //#ifdef ASHEN 211 // int d = 50; 212 //#else 213 // int d = 10; 214 //#endif 215 // printf("%d\n", d); 216
217 //第二种形式 218 //#ifndef ASHEN 219 // int d = 10; 220 //#else 221 // int d = 20; 222 //#endif 223 //
224
225 //第三种形式 226 //#if !1 227 // int b = 10; 228 //#else 229 // int b = 20; 230 //#endif 231
232
233 //六、枚举 234 //************************* 235 //罗列出所有的可能性 236 //枚举中的枚举值都是一个一个的标志符,不是变量,都有对应的整数值 237 // enum season{ 238 // Spring, //枚举值:春,夏,秋, 冻 239 // Summer = 100,//此时不是赋值,是对应关系 240 // Autumn = 200, 241 // Winter 242 // }; 243 // typedef enum season Sea; 244 // // Sea s = Winter;//定义枚举变量 245 //
246 // // printf("%d",s); 247 //
248 // Sea number = 0; 249 // printf("请输入季节(e.g. 春(0),夏季(100),秋季(200),冬季(201):"); 250 // scanf("%d",&number); 251 //
252 // switch (number) { 253 // case Spring: 254 // printf("春暖花开~~\n"); 255 // break; 256 // case Summer: 257 // printf("太热了~~\n"); 258 // break; 259 // case Autumn: 260 // printf("金黄的果子~~\n"); 261 // break; 262 // case Winter: 263 // printf("鹅毛飞雪~~\n"); 264 // break; 265 // default: 266 // printf("不看题呀你!!!\n"); 267 // break; 268 // } 269
270 //逗号表达式 271 //************************* 272 //c语言提供一种特殊的运算符,逗号运算符,优先级别最低,它将两式联接起来,如:(3+5,6+8)称为逗号表达式,其求解过程先表达式1,后表达式2,整个表达式值是表达式2的值,如:(3+5,6+8)的值是14,(a=3*5,a*4)的值是60,原因在于赋值运算优先级高于逗号表达式。 273 // int a = 0, x = 0, y = 0; 274 // //a = 2 * (x = y = 20, x * 3); 等价于以下式子 275 // a = 2 * ({ 276 // x = y = 20; 277 // x * 3; 278 // }); 279 // printf("%d\n", a); 280
281
282 // //#define kMaxValue(A, B) A > B ? A : B 283 // //#define kMaxValue1(A, B) ({int __a = A; int __b = B; __a > __b ? __a : __b;}) 284 // // x++ > y++ ? x++ : y++ 285 // //({typeof(x++) __a = x++; typeof(y++) __b = y++; __a > __b ? __a : __b;}) 286 // //typeof 获取参数的类型 287 // float x = 3, y = 5.6; 288 // float max = kMaxValue1(x++, y++); 289 // printf("max = %.2f\n", max); 290 // printf("x = %.2f\n", x); 291 // printf("y = %.2f\n", y); 292
293
294 //递归函数 295 //************************* 296 //本质:调用和自身功能完全相同的函数
297
298
2 |