// Filename: BIT1.CPP
// Demonstrate &, |, ^, and ~ on two values
#include <iostream.h>
void main()
{
int i = 5, j = 12;
int answer;
// Although this program applies the bitwise operators to
// variables, bitwise operators also work on literal values
answer = i & j;
cout << "The result of i & j is " << answer << endl;
answer = i | j;
cout << "The result of i | j is " << answer << endl;
answer = i ^ j;
cout << "The result of i ^ j is " << answer << endl;
// Notice how ~ takes only a single argument
answer = ~i;
cout << "The result of ~i is " << answer << endl;
answer = ~j;
cout << "The result of ~j is " << answer << endl;
return;
}
===========================
// Filename: BITEQUI2.CPP
// Uses bitwise operators to work with user input
#include <iostream.h>
void main()
{
int i, result;
char c;
cout << "Enter a number and I'll say if it's odd or even: ";
cin >> i;
if (i & 1)
{ cout << "The number is odd" << endl; }
else
{ cout << "The number is even" << endl; }
cout << endl << "Enter a number that you want multiplied by 32: ";
cin >> i;
result = i << 5; // Multiplies by 2 raised to the 5th power
cout << i << " times 32 is " << result << endl;
result = i >> 3;
cout << i << " divided by 8 is " << result << endl;
cout << endl << "Please enter your first initial: ";
cin >> c;
if (c & 32)
{ cout << "Always type your initial in uppercase" << endl; }
else
{ cout << "You entered the initial in uppercase, good!" << endl; }
return;
}
========================
// Filename: BITEQUIV.CPP
// Uses bitwise operators to work with user input
#include <iostream.h>
void main()
{
int i, result;
char c;
cout << "Enter a number and I'll say if it's odd or even: ";
cin >> i;
if ((i & 1) == 1)
{ cout << "The number is odd" << endl; }
else
{ cout << "The number is even" << endl; }
cout << endl << "Enter a number that you want multiplied by 32: ";
cin >> i;
result = i << 5; // Multiply by 2 raised to the fifth power
cout << i << " times 32 is " << result << endl;
cout << endl << "Please enter your first initial: ";
cin >> c;
if (c & 32)
{ cout << "Always type your initial in uppercase" << endl; }
else
{ cout << "You entered the initial in uppercase, good!" << endl; }
return;
}
=========================
// Filename: BITSHIFT.CPP
// Shows the results of several bitwise shift operations
#include <iostream.h>
void main()
{
int shiftResult;
shiftResult = 13 << 2;
cout << "13 << 2 is " << shiftResult << endl;
shiftResult = 13 >> 2;
cout << "13 >> 2 is " << shiftResult << endl;
shiftResult = -13 << 2;
cout << "-13 << 2 is " << shiftResult << endl;
shiftResult = -13 >> 2;
cout << "-13 >> 2 is " << shiftResult << endl;
return;
}
======================
// Filename: BONUSP1.CPP
// Shows how the advanced operators give
// you more control and efficiency
#include <iostream.h>
const int MAKEUPPER = 223;
void main()
{
char partCode;
int num, doubleNum;
float price;
double totalInv;
cout << "** Inventory Valuation **" << endl << endl;
cout << "What is the character part code? ";
cin >> partCode;
partCode &= MAKEUPPER; // Ensure that the part
// code is uppercase
cout << "How many parts are there? ";
cin >> num;
cout << "What is the price of each part? ";
cin >> price;
cout.precision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
totalInv = (double)num * (double)price;
cout << endl << "For part code " << partCode << ", ";
cout << "the total valuation is $" << totalInv << endl;
doubleNum = num << 1; // Compute double the current number
cout << endl << "Doubling our inventory would give us "
<< doubleNum << " new parts" << endl;
(num < 20) ? cout << endl << "We need to order more inventory!"
<< endl
:
cout << endl << "We now have enough inventory"
<< endl;
}
================
// Filename: ORANGE.CPP
// See if the user likes orange juice
#include <iostream.h>
void main()
{
const int UPMASK = 223;
char ans;
cout << "Do you like fresh squeeze orange juice (Y/N)? ";
cin >> ans;
ans &= UPMASK; // Convert to uppercase
if (ans == 'Y')
{ cout << "You'll be healthy all your life!" << endl; }
else
{ cout << "You need the vitamin C!" << endl; }
return;
}
Tidak ada komentar:
Posting Komentar