副标题#e#
Task1:二进制位操纵符
#include <stdio.h>
#define PRINT(int) printf(#int " = %d\n",int)
int main(void)
{
int x,y,z;
/*
优先级:位运算符中:& > |
位运算符& > 逻辑运算符&&
*/
x = 03; y = 02; z = 01;
PRINT(x | y & z); //0011 | 0000 = 3
PRINT(x | y & ~ z); //0011 | 0010 = 3
PRINT(x ^ y & ~ z); //0011 ^ 0010 = 1
PRINT(x & y && z); //2 && 1 = 1
x = 1; y = -1;
PRINT(! x | x); //((!x) | x)=false | x = 1
// http://www.bianceng.cn
PRINT(~ x | x); //1111 = -1
PRINT(x ^ x); //0001 ^ 0001 = 0
x <<= 3;PRINT(x); //左移n位结果上当作乘以n个2 = 8
y <<= 3;PRINT(y); //………………………………&hellip ;= -8
y >>= 3;PRINT(y); //右移n位结果上当作除以n个2 = -1
return 0;
}
输出功效:
x | y & z = 3
x | y & ~ z = 3
x ^ y & ~ z = 1
x & y && z = 1
! x | x = 1
~ x | x = -1
x ^ x = 0
x = 8
y = -8
y = -1
#p#副标题#e#
Task2:干系操纵符和条件操纵符
#include <stdio.h>
#define PRINT(int) printf(#int " = %d\n",int)
int main(void)
{
int x = 1, y = 1, z = 1;
x += y += z; //右团结,x = 3, y = 2, z = 1
PRINT( x < y ? y : x); //x = 3
PRINT( x < y ? x ++ : y ++); //输出2,y = 3
PRINT(x);PRINT(y);//3,3
PRINT( z += x < y ? x ++ : y ++); //输出4,z = z+3 = 4,y = 4
PRINT(y);PRINT(z);//4,4
x = 3; y = z = 4;
PRINT((z >= y >= x) ? 1 : 0); //(1>=3)?1:0 = 0
PRINT( z >= y && y >= x); //(z>=y)&&(y>=x)=true&&true=1
return 0;
}
输出功效:
x < y ? y : x = 3
x < y ? x ++ : y ++ = 2
x = 3
y = 3
z += x < y ? x ++ : y ++ = 4
y = 4
z = 4
(z >= y >= x) ? 1 : 0 = 0
z >= y && y >= x = 1
Task3:操纵符的优先级和求值顺序
#include <stdio.h>
#define PRINT3(x,y,z) printf(#x "=%d\t" #y "=%d\t" #z "=%d\n",x,y,z);
int main(void)
{
int x,y,z;
x = y = z = 1;
++x || ++y && ++z; PRINT3(x,y,z); //2,1,1
x = y = z = 1;
++x && ++y || ++z; PRINT3(x,y,z); //2,2,1
x = y = z = 1;
++x && ++y && ++z; PRINT3(x,y,z); //2,2,2
x = y = z = -1;
++x && ++y || ++z; PRINT3(x,y,z); //0,-1,0
x = y = z = -1;
++x || ++y && ++z; PRINT3(x,y,z); //0,0,-1
x = y = z = -1;
++x && ++y && ++z; PRINT3(x,y,z); //0,-1,-1
return 0;
}
输出功效:
x=2 y=1 z=1
x=2 y=2 z=1
x=2 y=2 z=2
x=0 y=-1 z=0
x=0 y=0 z=-1
x=0 y=-1 z=-1
查察本栏目