Task1: Bits operator
#include <stdio.h>
#define PRINT (int) printf (#int "=%d\n", int)
int main (void)
{
int x,y,z;
/*
Priority: Bitwise operator:& > |
Bitwise operators & > Logical Operators &&
*/
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); The left-shifted n-bit effect is considered multiplied by n 2 = 8
Y <<= 3; PRINT (y); ... &hellip------------------------8
Y >>= 3; PRINT (y); Right shift n-bit effect as divided by n 2 =-1
return 0;
}
Output results:
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