Bitwise Operators Andy Wang Object Oriented Programming in
Bitwise Operators Andy Wang Object Oriented Programming in C++ COP 3330
Bits and Bytes A bit is the smallest unit of storage in a computer A byte consists of 8 bits 1 byte on most systems What if we want to access individual bits? Smallest unit of directly addressable storage The smallest built-in data type is the char It store 0 or 1 Must use the bitwise operators Caution: bitwise operators may be machine dependent Most machines use 2’s complement format
Base Conversion 1510 -> 11112 15 % 2 = 1 // least significant bit 15 / 2 = 7 7 % 2 = 1 // next bit 7/2=3 3 % 2 = 1 // next bit 3/2=1 1 % 2 = 1 // next bit 1 / 2 = 0 // terminate
Base Conversion 11112 1510 1 x 23 + 1 x 22 + 1 x 21 + 1 x 20 = 8 + 4 + 2 + 1 = 15
Two’s Complement Suppose we have 4 -bit integers Non-negative numbers Just convert base 10 numbers to base 2 010 = 00002 110 = 00012 210 = 00102 To add two numbers, just add the corresponding bits 110 + 210 = 00012 + 00102 = 00112 = 1 x 21 + 1 x 20 = 310
Two’s Complement Negative numbers Flip all the bits of a non-negative numbers, then add 1 -110 = flip(00012) + 1 = 11102 + 1 = 11112 010 = flip(00002) + 1 = 11112 + 1 = 00002 If the uppermost bit is one, the number is negative To add a non-negative number with a negative number, just add 110 + (-1)10 = 00012 + 11112 = 00002 = 0
Two’s Complement To add two negative numbers, just add as well (-1)10 + (-1)10 = 11112 + 11112 = 11102 To decode a negative number, subtract the number by 1, and flip all the bits 11102 = negative flip(11102 – 1) = negative flip(11012) = negative 00102 = negative (1 x 21) = negative 210
The Bitwise Operators Operator Name Arity Description & Bitwise AND Binary Similar to the && operator, but on a bit-by-bit basis. | Bitwise OR Binary Similar to the || operator, but on a bit-by-bit basis. ^ Bitwise Exclusive OR Binary Set to 1 if one of the corresponding bits is 1, or set to 0 otherwise. ~ Complement Unary Flips the bits in the operand.
The Bitwise Operators Operator Name Arity Description << Left shift Binary Shifts the bits of the first operand to the left by the number of bits specified in the second operand. Right fill with 0 bits. >> Right shift Binary Shifts the bits of the first operand to the right by the number of bits specified in the second operand. Left fill with 0’s for positive numbers, 1’s for negatives (machine dependent).
Shortcut Assignment Operators x &= y means x = x & y x |= y means x = x | y x ^= y means x = x ^ y x <<= y means x = x << y x >>= y means x = x >> y
Examples Suppose we have the following code short x = 6891; short y = 11318; Assume short is 2 bytes (16 bits) x: 00011010 11101011 y: 00101100 00110110 -----------x & y:
Examples Suppose we have the following code short x = 6891; short y = 11318; Assume short is 2 bytes (16 bits) x: 00011010 11101011 y: 00101100 00110110 -----------x & y: 00001000 0010 (2082)
Examples x: 00011010 11101011 y: 00101100 00110110 ------------x | y: x: 00011010 11101011 y: 00101100 00110110 ------------x ^ y:
Examples x: 00011010 11101011 y: 00101100 00110110 ------------x | y: 00111110 1111 (16127) x: 00011010 11101011 y: 00101100 00110110 ------------x ^ y:
Examples x: 00011010 11101011 y: 00101100 00110110 ------------x | y: 00111110 1111 (16127) x: 00011010 11101011 y: 00101100 00110110 ------------x ^ y: 00110110 1101 (14045)
Examples x: 00011010 11101011 --------------x << 2: y: 00101100 00110110 ------------y >> 4: x: 00011010 11101011 ------------~x:
Examples x: 00011010 11101011 --------------x << 2: 0110101100 (27564) y: 00101100 00110110 ------------y >> 4: x: 00011010 11101011 ------------~x:
Examples x: 00011010 11101011 --------------x << 2: 0110101100 (27564) y: 00101100 00110110 ------------y >> 4: 00000010 11000011 (707) x: 00011010 11101011 ------------~x:
Examples x: 00011010 11101011 --------------x << 2: 0110101100 (27564) y: 00101100 00110110 ------------y >> 4: 00000010 11000011 (707) x: 00011010 11101011 ------------~x: 11100101 00010100 (-6892)
Code Examples http: //www. cs. fsu. edu/~myers/cop 3330/examples/bit wise/ex 1. cpp
Code Examples #include <iostream> #include <iomanip> using std: : cout; using std: : endl; int main() { short x = 6891, y = 11318; cout << "x = " << x << "ny = " << y << endl; cout cout } << << << "x & y = " << (x & y) << endl; "x | y = " << (x | y) << endl; "x ^ y = " << (x ^ y) << endl; "x << 2 = " << (x << 2) << endl; "y >> 4 = " << (y >> 4) << endl; "~x = " << ~x << endl;
Examples from Dietel http: //www. cs. fsu. edu/~myers/deitel 5 c++/ch 22/Fig 22_ 06/fig 22_06. cpp http: //www. cs. fsu. edu/~myers/deitel 5 c++/ch 22/Fig 22_ 08/fig 22_08. cpp Display bit values &, |, ^, and ~ bitwise operators http: //www. cs. fsu. edu/~myers/deitel 5 c++/ch 22/Fig 22_ 11/fig 22_11. cpp << and >> bitwise operators
fig 22_06. cpp #include <iostream> #include <iomanip> using namespace std; void display. Bits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; // 1000…. cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘ 1’; value <<= 1; else cout << ‘ 0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl; }
fig 22_06. cpp int main() { unsigned input. Value; cout << “Enter an unsigned integer: “; cin >> input. Value; display. Bits(input. Value); return 0; }
fig 22_08. cpp #include <iostream> #include <iomanip> using namespace std; void display. Bits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘ 1’; value <<= 1; else cout << ‘ 0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl; }
fig 22_08. cpp int main() { unsigned number 1, number 2, mask, set. Bits; number 1 = 179876355; mask = 1; cout << “The result of combining the followingn”; display. Bits(number 1); display. Bits(mask); cout << “using the bitwise AND operator & isn”; display. Bits(number 1 & mask); number 1 = 15; set. Bits = 241; cout << “The result of combining the followingn”; display. Bits(number 1); display. Bits(set. Bits); cout << “using the bitwise OR operator | isn”; display. Bits(number 1 | set. Bits);
fig 22_08. cpp number 1 = 139; number 2 = 199; cout << “The result of combining the followingn”; display. Bits(number 1); display. Bits(number 2); cout << “using the bitwise exclusive OR operator ^ isn”; display. Bits(number 1 ^ number 2); number 1 = 21845; cout << “n. The one’s complement ofn”; display. Bits(number 1); cout << “is”; display. Bits(~number 1); return 0; }
fig 22_11. cpp #include <iostream> #include <iomanip> using namespace std; void display. Bits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘ 1’; value <<= 1; else cout << ‘ 0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl; }
fig 22_11. cpp int main() { int number 1 = -2000; cout << “The result of left shiftingn”; display. Bits(number 1); cout << “ 8 bit positions using the left-shift operator isn”; display. Bits(number 1 << 8); cout << “n. The result of right shiftingn”; display. Bits(number 1); cout << “ 8 bit positions using the right-shift operator isn”; display. Bits(number 1 >> 8); return 0; }
Bit. Flags Examples http: //www. cs. fsu. edu/~myers/cop 3330/examples/bit wise/bitflags/ Set up to store a set of on/off flags On a system with 4 -byte integers, a Bit. Flag object can store 32 flags (using one variable for all flags)
bitflags. h class Bit. Flags { public: Bit. Flags(); void Set(int num); void Unset(int num); void Flip(int num); bool Query(int num) const; private: int Mask(int num) const; unsigned int flags; const int numflags; };
bitflags. cpp #include <iostream> #include “bitflags. h” Bit. Flags: : Bit. Flags() : numflags(sizeof(int)*8) { flags = 0; } int Bit. Flags: : Mask(int num) const { return (1 << num); } void Bit. Flags: : Set(int num) { flags = flags | Mask(num); }
bitflags. cpp void Bit. Flags: : Unset(int num) { flags = flags & ~Mask(num); } void Bit. Flags: : Flip(int num) { flags = flags ^ Mask(num); } Bool Bit. Flags: : Query(int num) { return (flags & Mask(num)); }
main. cpp #include <iostream> #include “bitflags. h” using namespace std; int main() { Bit. Flags b; for (int j = 0; j < 32; j += 2) b. Set(j); for (int j = 0; j < 32; j++) // reverse order… cout << b. Query(j); cout << endl;
main. cpp b. Set(5); b. Unset(8); b. Flip(31); for (int j = 0; j < 32; j++) cout << b. Query(j); cout << endl; return 0; }
- Slides: 35