I feel like a dork asking this question, but I need help. The book I am using is
ID: 3624746 • Letter: I
Question
I feel like a dork asking this question, but I need help. The book I am using is Deitel's C: How to Program 6th ed. The question I am stuck on is chapter 10 exercise 17--it reads as follows: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 were reversed properly.
Thank you ahead of time for the help.
Explanation / Answer
please rate - thanks
#include<stdio.h>
#include <conio.h> //needed for getch below
void reversebits(int);
void outputbits(int);
int main()
{unsigned int number;
int i;
printf("Enter a number ");
scanf("%d",&number);
outputbits(number);
reversebits(number);
getch(); //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)
printf("0");
else
printf("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)
printf("0");
else
printf("1");
mask=mask*2; //move to the left
}
return ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.