(Multiplication Via Bit Shifting) Left-shifting an unsigned integer by 1 bit is equivalent to multiplying the value by 2. Write function power2 that takes two integer arguments, number and pow, and calculates number * 2pow Use a shift operator to calculate the result. The program should print the values as integers and as bits.
What will be an ideal response?
```
#include
#include
using namespace std;
void displayBits( unsigned ); // prototype
unsigned power2( unsigned, unsigned ); // prototype
int main()
{
unsigned number;
unsigned pow;
unsigned result;
cout << "Enter two integers: ";
cin >> number >> pow;
cout << "number:\n";
displayBits( number );
cout << "\npower:\n";
displayBits( pow );
result = power2( number, pow );
cout << '\n' << number << " * 2^" << pow << " = " << result << '\n';
displayBits( result );
return 0;
} // end main
// shifting n
unsigned power2( unsigned n, unsigned p )
{
return n << p; // shift n to the left p times
} // end function power2
// display the bits of value
void displayBits( unsigned value )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1 << SHIFT;
cout << setw( 7 ) << value << " = ";
for ( unsigned c = 1; c <= SHIFT + 1; c++ )
{
cout << ( value & MASK ? '1' : '0' );
value <<= 1; // shift value left by 1
if ( c % 8 == 0 ) // output a space after 8 bits
cout << ' ';
} // end for
cout << endl;
} // end function displayBits
```
Enter two integers: 4 7
number:
4 = 00000000 00000000 00000000 00000100
power:
7 = 00000000 00000000 00000000 00000111
4 * 2^7 = 512
512 = 00000000 00000000 00000010 00000000
You might also like to view...
Write a program to answer questions like the following: Suppose the species Klingon ox has a population of 100 and a growth rate of 15 percent, and it lives in an area of 1500 square miles. How long would it take for the population density to exceed 1 per square mile? Use the class Species in Listing 5.19 with the addition of the getDensity method from Self-Test Question 10.
This project requires a new version of Species that includes the density method from Self-Test Question 10. The main program uses this new version of the class and does the following: asks the user to enter data for the species (name, population and growth rate), the area (in square miles), and the target density. Note that the solution in this manual is generalized so that the user can enter any density; it is not hard-coded for 1 per square mile. Also note that a check is made to see if the density is already at or above the target before it enters the loop to calculate the number of years.
In a PivotChart report, any ________ can be assigned to either axis
A) field B) record C) value D) label
Dynamic IP addresses are provided to a computer when it needs to be connected to the network. The provider is the DHCP server. When the computer is disconnected, the IP address becomes available for use by another computer. The address does not become available immediately, however. It is leased for a specific period of time, and when the lease is up, the IP address is placed back in an IP
address pool and can be delivered to another computer. Indicate whether the statement is true or false
All of the following are alert styles that appear if a user enters invalid data EXCEPT ____.?
A. ?Abort B. ?Information C. ?Warning D. ?Stop