Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

Similar presentations


Presentation on theme: "1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise."— Presentation transcript:

1 1 Bitwise Operators

2 2 Bits and Constants

3 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement" operator ~

4 4 ExpressionRepresentationValue a00000000 00000000 10000010 0011010133333 b11111111 11111110 11010000 00101111-77777 a & b00000000 00000000 10000000 0010010132805 a ^ b11111111 11111110 01010010 00011010-110054 a | b11111111 11111110 11010010 00111111-77249 ~ ( a | b )00000000 00000001 00101101 1100000077248 ~ a & ~ b00000000 00000001 00101101 1100000077248 Representations int a = 33333, b = -77777; The value of each bit is determined only by the bit(s) in its position

5 5 Left Shifts short a = 0x68ab;... a <<= 3; /* shift left 3 bits */ Same as: a = a << 3; http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm Bits positions vacated by shift are filled with zeros

6 6 Right Shifts - Unsigned unsigned short a = 0x98ab;... a >>= 5; /* shift right 5 bits */ http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm For unsigned data type, bits positions vacated by shift are filled with zeros.

7 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 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 9 Expressio n RepresentationAction c00000000 00000000 00000000 01011010unshifted c << 400000000 00000000 00000101 10100000left shifted 4 a10000000 00000000 00000000 00000000unshifted a >> 311110000 00000000 00000000 00000000right shifted 3 b10000000 00000000 00000000 00000000unshifted b >> 300010000 00000000 00000000 00000000right 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 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 0x0040 0000000001000000 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000

11 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: 00000000 00000000 00000000 00000111 (MSB) (LSB)

12 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 = 100...0 */ 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 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 = 0 0 0 a p = 0 0 a 0 0 0 0 b 0 0 a b 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 Most significant Least significant

14 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 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)


Download ppt "1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise."

Similar presentations


Ads by Google