220 likes | 576 Views
Bitwise Operators. Bitwise Operators (integers). Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement" operator ~ Shift left << Shift right >>. Base 2 and 2's complement. 2's complement: x+(-x) = 2^n where n = #bits.
E N D
Bitwise Operators (integers) • Bitwise "and" operator & • Bitwise "or" operator | • Bitwise "exclusive or" operator ^ • Bitwise "ones complement" operator ~ • Shift left << • Shift right >>
Base 2 and 2's complement 2's complement: x+(-x) = 2^nwhere n = #bits
operators &, |, ^,~ The value of each bit is determined only by the bit(s) in its position • Examples: int a = 33333, b = -77777;
Operator precedence and associativity - final look (a+b << 12*a >> b) is equivalent to (((a+b)<<(12*a))>>b)
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 http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm
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. http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm
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
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
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
0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 Implementation Note 0x0001 0x0002 0x0004 0x0008 0x0010 0x0020 0x0040 • x<<n is equivalent to multiplication by 2n. • x>>n is equal to x/2n • Shifting is much faster than actual multiplication (*) or division (/)==> Multiplications / divisions by powers of 2 should be implemented using shifts.
Printing the bits of an integer 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; } Prints the binary representation of an integer. E.g: 00000000 00000000 00000000 00000111(MSB) (LSB)
n is the number of bits in an integer #include <limits.h> void bit_print(int a){ int i; int n = sizeof(int) * CHAR_BIT; /* #define CHAR_BIT 8 (in <limits.h>)*/ int mask = 1 << (n - 1); /* mask = 100...0 */ for (i = 1; i <= n; ++i) { putchar((a & mask)? '1' : '0'); a <<= 1; if (i % CHAR_BIT == 0 && i < n) putchar(' '); } } Prints the most significant bit of a (condition) ? (if true) : (if false) if (a&mask == 0) putchar(`0`); else putchar(`1`); i % 8 == i & 7 Prints a space between the bytes
Pack 4 chars into an int #include <limits.h> 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; } Least significant Most significant p = 0 0 a 0 0 0 0 b 0 0 a b p = 0 0 0 a p = 0 a b 0 0 0 0 c 0 a b c p = a b c 0 0 0 0 d a b c d
Unpack a byte from an int #include <limits.h> 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 kth byte is on