(Shifting and Printing an Integer) Write a program that right-shifts an integer variable 4 bits. The program should print the integer in bits before and after the shift operation. Does your system place zeros or ones in the vacated bits?

What will be an ideal response?

```
// Program that right-shifts an integer variable 4 bits.
#include
#include
using namespace std;

void displayBits( unsigned ); // prototype

int main()
{
unsigned val;

cout << "Enter an integer: ";
cin >> val;

cout << "Before right shifting 4 bits is:\n";
displayBits( val );
cout << "After right shifting 4 bits is:\n";
displayBits( val >> 4 );
return 0;
} // end main

// 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;

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

cout << endl;
} // end function displayBits
```
Enter an integer: 5345
Before right shifting 4 bits is:
5345 = 00000000 00000000 00010100 11100001
After right shifting 4 bits is:
334 = 00000000 00000000 00000001 01001110

Computer Science & Information Technology

You might also like to view...

A ________ allows a user to view multiple folders in File Explorer simultaneously

A) file B) dialog box C) library D) Zip file

Computer Science & Information Technology

In the radial-gradient function, the default is to place the gradient within the center of a background.?

Answer the following statement true (T) or false (F)

Computer Science & Information Technology