1 Bitwise Operators
2 Bits and Constants
3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement" operator ~
4 ExpressionRepresentationValue a b a & b a ^ b a | b ~ ( a | b ) ~ a & ~ b Representations int a = 33333, b = ; The value of each bit is determined only by the bit(s) in its position
5 Left Shifts short a = 0x68ab;... a <<= 3; /* shift left 3 bits */ Same as: a = a << 3; Bits positions vacated by shift are filled with zeros
6 Right Shifts - Unsigned unsigned short a = 0x98ab;... a >>= 5; /* shift right 5 bits */ For unsigned data type, bits positions vacated by shift are filled with zeros.
7 Right Shifts - Signed (machine dependent) short a = 0x98ab;... a >>= 5; /* shift right 5 bits */ Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type
8 Right Shifts (Signed) short a = 0x78ab;... a >>= 5; /* shift right 5 bits */ Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type
9 Expressio n RepresentationAction c unshifted c << left shifted 4 a unshifted a >> right shifted 3 b unshifted b >> right shifted 3 Representations char c = 'Z'; int a = 1 << 31; /* shift 1 to the high bit */ unsigned b = 1 << 31; For signed data types bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type
10 Implementation Note x<<n is equivalent to multiplication by 2 n. x>>n is equal to x/2 n Shifting is much faster than actual multiplication (*) or division (/) ==> Multiplications / divisions by powers of 2 should be implemented using shifts. 0x0001 0x0002 0x0004 0x0008 0x0010 0x0020 0x
11 int main(void){ int num=0; do{ printf("Enter an integer:\n"); scanf("%d", &num); bit_print(num); putchar('\n'); } while (num!=0); return 0; } Printing the bits of an integer Prints the binary representation of an integer. E.g: (MSB) (LSB)
12 #include void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* #define CHAR_BIT 8 (in )*/ int mask = 1 << (n - 1); /* mask = */ for (i = 1; i <= n; ++i) { putchar((a & mask)? '1' : '0'); a <<= 1; if (i % CHAR_BIT == 0 && i < n) putchar(' '); } n is the number of bits in an integer Prints the most significant bit of a Prints a space between the bytes (condition) ? (if true) : (if false) if (a&mask == 0) putchar(`0`); else putchar(`1`); i % 8 == i & 7
13 Pack 4 chars into an int #include int pack( char a, char b, char c, char d ) { int p = a; p = (p << CHAR_BIT) | b; p = (p << CHAR_BIT) | c; p = (p << CHAR_BIT) | d; return p; } p = a p = 0 0 a b 0 0 a b p = 0 a b c 0 a b c p = a b c d a b c d Most significant Least significant
14 Unpack a byte from an int #include char unpack( int p, int k ) { unsigned mask = 0xFF; int n = k * CHAR_BIT; mask <<= n; return ( ( p & mask ) >> n ); } k = 0, 1, 2 or 3 n = 0, 8, 16 or 24 k th byte is on
15 OperatorsAssociativity () []. -> ++(postfix) --(postfix)left to right +(unary) -(unary) ++(prefix) --(prefix) ! sizeof(type) &(address) *(dereference) ~ right to left * / %left to right + -left to right >left to right >=left to right == !=left to right & ^ | &&left to right ||left to right ?:right to left = += -= *= /= &= >>= etcright to left,(comma operator)left to right Operator precedence and associativity - final look (a+b > b) is equivalent to (((a+b) >b)