1 / 15

Bitwise Operators

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.

tala
Download Presentation

Bitwise Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bitwise Operators

  2. Bitwise Operators (integers) • Bitwise "and" operator & • Bitwise "or" operator | • Bitwise "exclusive or" operator ^ • Bitwise "ones complement" operator ~ • Shift left << • Shift right >>

  3. Base 2 and 2's complement 2's complement: x+(-x) = 2^nwhere n = #bits

  4. operators &, |, ^,~ The value of each bit is determined only by the bit(s) in its position • Examples: int a = 33333, b = -77777;

  5. Operator precedence and associativity - final look (a+b << 12*a >> b) is equivalent to (((a+b)<<(12*a))>>b)

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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.

  12. 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)

  13. 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

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

  15. 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

More Related