(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...

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

1) To declare an iterator, one must #include the proper header file, then specify the container type and use that with the scope resolution operator, ::, to qualify the inner type iterator, to declare the iterator variable, as in #include std::vector::iterator myIterator;. 2) You can assign stacks of the same base type 3) You cannot copy stacks. 4) The template stack and queue adapters have a copy constructor, an overloaded operator assignment, and a destructor.

Computer Science & Information Technology

Everything on the Internet is unquestionably protected by copyright law

Indicate whether the statement is true or false

Computer Science & Information Technology

Forms are originally formatted with the dataset theme.

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

Computer Science & Information Technology

Which of the following is not checked by the Accessibility Checker?

A) Merged Cells B) Print Area C) Table Headers D) Alt Text

Computer Science & Information Technology