(Reversing Bits) Write a program that reverses the order of the bits in an unsigned integer value. The program should input the value from the user and call function reverseBits to print the bits in reverse order. Print the value in bits both before and after the bits are reversed to confirm that the bits are reversed properly.

What will be an ideal response?


```
#include
#include
using namespace std;

// prototypes
void reverseBits( unsigned * const );
void displayBits( unsigned );

int main()
{
unsigned a;

cout << "Enter an unsigned integer: ";
cin >> a;

// before call to reverseBits
cout << "\nBefore bits are reversed:\n";
displayBits( a );

// after call to reverseBits
reverseBits( &a );
cout << "\nAfter bits are reversed:\n";
displayBits( a );

return 0;
} // end main

// reverse the bits
void reverseBits( unsigned * const vPtr )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1;
unsignedvalue = *vPtr;

for ( int i = 0; i <= SHIFT; i++ )
{
*vPtr <<= 1; // left shift the pointer
*vPtr |= ( value & MASK ); // inclusive-OR operator
value >>= 1; // right shift
} // end for
} // end function reverseBits

// display the bits
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;

if ( c % 8 == 0 )
cout << ' ';
} // end for

cout << endl;
} // end function displayBits
```
Enter an unsigned integer: 330
Before bits are reversed:
330 = 00000000 00000000 00000001 01001010
After bits are reversed:
21648000 = 00000001 01001010 01010010 10000000

Computer Science & Information Technology

You might also like to view...

When working with the position property, the ________ value allows you to place an element in a defined location within a browser window

A) absolute B) area C) relative D) placement

Computer Science & Information Technology

The Web Credentials store is integrated with _________ and used to store any saved passwords for websites, FTP sites, and other Internet services accessed through the web browser?

a. Group Policy b. Active Directory c. Internet Explorer d. Windows Explorer

Computer Science & Information Technology

Before data is transmitted, it is broken into chunks of data and then encapsulated with the source and destination IP addresses to create a ____________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

When creating a bivariate table, the ________ variable should be placed along the top of the table, and the ________ variable should be placed along the side of the table

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology