// Bitwise operator demo program // Mr. Minich // Purpose - to demonstrate the use of the bitwise operators &, |, and ~ #include<iostream.h> #include <math.h> struct bitstring // must use a struct to access bits { unsigned value: 5; // bit field of 5 bits, could be changed to any size }; // in order to work with bitstrings of different // lengths int main() { int nextDigit; // used in decimal to binary conversion bitstring input1, input2, input3, result; // bitstrings input1.value = 3; // 3 in decimal is 00011 in binary input2.value = 5; // 5 in decimal is 00101 in binary input3.value = 10; // 10 in decimal is 00101 in binary result.value = input1.value & input2.value; cout << "00011 AND 00101 = " << (input1.value & input2.value) << " as a decimal" << endl; cout << "00011 OR 00101 = " << (input1.value | input2.value) << " as a decimal" << endl; input3.value = ~input3.value;
cout << "NOT 01010 = "; for (int i = 4; i >= 0; i--) // converting a decimal to a binary value { nextDigit = input3.value / pow(2, i); input3.value = input3.value % int (pow(2, i)); cout << nextDigit; } // end of for cout << " in binary" << endl << endl; return 0; }// end of main