Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reverses the order of the bits in an unsigned integer value

ID: 3621401 • Letter: W

Question

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.

Here is the function reverseBits (I think that the function is correct):

unsigned reverseBits(unsigned value)
{
unsigned maske =1;/* bit mask */
unsigned temp = 0;/* reversed bits */
int i;/* loop counter */

/* loop through bits of value */
for(i =0; i<=15; i++){
temp <<= 1; /* right shift 1 bit */
temp |= (value & mask); /* separate bit and plave in temp */
value >>=1; /* left shift 1 bit */
} /* end for */

return temp;
}

Explanation / Answer

please rate - thanks

your assignment says reverseBits prints the bits in reverse order


#include<iostream>
using namespace std;
void reversebits(int);
void outputbits(int);
int main()
{unsigned int number;
int i;
cout<<"Enter a number ";
cin>>number;
outputbits(number);
reversebits(number);          
system("pause");                   //prevents DOS window from closing so you can see your results
return 0;
}
void outputbits(int number)
{unsigned int mask=0x8000,result;
int i;
for(i=0;i<16;i++)
   {result=number & mask;   //starting at the left AND each bit with 1 printing the results for that bit
    if(result==0)
        cout<<"0";
    else
        cout<<"1";
    mask=mask/2;           //move to the right
    }
return ;
}     
void reversebits(int number)
{unsigned int mask=0x0001,result;
int i;
printf(" The number reversed is: ");
for(i=0;i<16;i++)
   {result=number & mask;      //starting at the right AND each bit with 1 printing the results for that bit        
    if(result==0)
         cout<<"0";
    else
        cout<<"1";
    mask=mask*2;               //move to the left
    }
cout<<endl;
return ;
}     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote