hello i have c assignment homework could you help me there are one text and one
ID: 3725207 • Letter: H
Question
hello i have c assignment homework could you help me
there are one text and one image
operations.c :
Explanation / Answer
Answer to part 2 : reverseBits(value) function :--
// c program to reverse bits of a given integer .
#include <stdio.h>
// reverseBits function .
int reverseBits(int n)
{
int a,b=0 ;
while(n > 0) // while loop .
{
a = n % 10 ; // a stores the remainder of division of n by 10 .
n = n / 10; // n is divided by 10 to reduce (discard) the end digit .
b = 10 * b + a ; // a is added to b to reverse the Bits .
}
printf("%d",b);
return 0;
};
int main(void) {
unsigned int n ;
scanf("%d",&n); // taking input n .
printf("Before reversing the bits the value was %d ",n);
printf(" After reversing the bits the value becomes ");
reverseBits(n); // calling function reverseBits .
return 0;
}
Answer to part 3 : power2(number, pow) function :--
// c program to implement function power2(number, pow)
#include <stdio.h>
// function to convert decimal to binary .
unsigned fn(unsigned k) {
if (k == 0) return 0;
if (k == 1) return 1;
return (k % 2) + 10 * fn(k / 2);
};
// function power2(number, pow)
int power2(long long int n, int p)
{
while(p > 0) // while loop running p times thus p times doing left shift
{ // and thus the number gets multiplied by 2 p times i.e. number * 2pow .
n = n << 1 ; // left shifting numer by 1 bit
p--; // decrement p .
}
printf("the result is = %lld and in bitwise it is = %d ",n,fn(n));
return 0;
};
int main(void) {
int n,p ;
printf("Enter a number and power ");
scanf("%d %d",&n,&p);
printf("the value is = %d and in bitwise it is = %d ",n,fn(n));
power2(n, p); // calling function power2 .
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.