Exercise 3-12 (Boolean and Bitwise with explicit operators)
Chapter_3 Exercise_3-11 Exercise_3-8 | typedef Exercise_3-13 |
Exercise 3-12 TCP1, p. 228
Exercise 3-12. Modify ch3-Boolean and ch3-Bitwise (Section ch3-Operators) so they use the explicit operators (if your compiler is conformant to the C++ Standard it will support these).
CONTENTS: ExBoolean.cpp ExBitwise.cpp
ExBoolean.cpp TCP1, p. 171-172, 187 download
// Relational and logical explicit operators
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int i,j;
cout << "Enter an integer: ";
cin >> i;
cout << "Enter another integer: ";
cin >> j;
cout << "i > j is " << (i > j) << endl;
cout << "i < j is " << (i < j) << endl;
cout << "i >= j is " << (i >= j) << endl;
cout << "i <= j is " << (i <= j) << endl;
cout << "not(i > j) is " << not(i > j) << endl; // i <= j
cout << "i == j is " << (i == j) << endl;
cout << "i not_eq j is " << (i not_eq j) << endl;
cout << "i and j is " << (i and j) << endl;
cout << "i or j is " << (i or j) << endl;
cout << "(i < 10) and (j < 10) is " << ((i < 10) and (j < 10)) << endl;
}
/*
g++ ExBoolean.cpp -o ExBoolean
./ExBoolean
Enter an integer: 0
Enter another integer: 0
i > j is 0 // false
i < j is 0
i >= j is 1 // true
i <= j is 1
not(i > j) is 1 // i <= j
i == j is 1
i not_eq j is 0
i and j is 0
i or j is 0
(i < 10) and (j < 10) is 1
./ExBoolean
Enter an integer: 1
Enter another integer: 2
i > j is 0
i < j is 1
i >= j is 0
i <= j is 1
not(i > j) is 1 // i <= j
i == j is 0
i not_eq j is 1
i and j is 1
i or j is 1
(i < 10) and (j < 10) is 1
./ExBoolean
Enter an integer: 10
Enter another integer: 9
i > j is 1
i < j is 0
i >= j is 1
i <= j is 0
not(i > j) is 0 // i <= j
i == j is 0
i not_eq j is 1
i and j is 1
i or j is 1
(i < 10) and (j < 10) is 0
*/
ExBitwise.cpp TCP1, p. 174, 187-188 download
// Demonstration of bit manipulation with explicit operators
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void printBinary(const unsigned char val); // Display a byte in binary
// A macro to save typing:
#define PR(STR, EXPR) \
cout << STR; printBinary(EXPR); cout << endl;
int main()
{
unsigned int getval;
unsigned char a, b;
cout << "Enter a number between 0 and 255: ";
cin >> getval; a = getval;
PR("a in binary: ", a) // semicolon not required here
cout << "Enter a number between 0 and 255: ";
cin >> getval; b = getval;
PR("b in binary: ", b)
PR("a bitor b = ", a bitor b)
PR("a bitand b = ", a bitand b)
PR("a xor b = ", a xor b)
PR("compl a = ", compl a)
PR("compl(b) = ", compl(b))
// An interesting bit pattern:
unsigned char c = 0x5A; // 90
PR("c in binary: ", c)
a or_eq c;
PR("a or_eq c; a = ", a)
b and_eq c;
PR("b and_eq c; b = ", b)
b xor_eq a;
PR("b xor_eq a; b = ", b)
}
// Display a byte in binary
void printBinary(const unsigned char val)
{
for(int i = 7; i >= 0; i--)
{ // print bits from first (most significant) to last (least significant)
if(val bitand (1 << i)) // set (1) bit
{cout << "1";}
else {cout << "0";} // 0 bit
}
}
/*
g++ ExBitwise.cpp -o ExBitwise
./ExBitwise
Enter a number between 0 and 255: 0
a in binary: 00000000
Enter a number between 0 and 255: 255
b in binary: 11111111
a bitor b = 11111111
a bitand b = 00000000
a xor b = 11111111
compl a = 11111111
compl(b) = 00000000
c in binary: 01011010
a or_eq c; a = 01011010
b and_eq c; b = 01011010
b xor_eq a; b = 00000000
./ExBitwise
Enter a number between 0 and 255: 26
a in binary: 00011010
Enter a number between 0 and 255: 105
b in binary: 01101001
a bitor b = 01111011
a bitand b = 00001000
a xor b = 01110011
compl a = 11100101
compl(b) = 10010110
c in binary: 01011010
a or_eq c; a = 01011010
b and_eq c; b = 01001000
b xor_eq a; b = 00010010
*/
Chapter_3 Exercise_3-11 Exercise_3-8 | BACK_TO_TOP | typedef Exercise_3-13 |
Comments
Post a Comment