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

Write a C program to run on ocelot which will flip a single bit in a number ente

ID: 3845927 • Letter: W

Question

Write a C program to run on ocelot which will flip a single bit in a number entered by the user using the binary representation of the number. The user should input the original integer value between 1 and 1000 using a scanf. Output is to the screen. For this assignment give the user directions asking them to enter the integer and then ask them which bit to flip. That can be a number between 0 and 31. After completing the operation ask the user if they want to do it again. You do not need to use getopt for this program. If the user enters a 1 as the initial value and a 0 for the bit to switch the result would be 0. If the user enters a 10 as the initial value and a 1 for the bit to switch then the result would be 8. . If the user enters a 10 as the initial value and a 2 for the bit to switch then the result would be 14. The program should compile to create an executable called bitswitch.

Explanation / Answer

Answer-> i write this program in c according to the requirements of the question .

---------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include<math.h>
void decimalToBinary(long n);
void numberAfterBitFlip(int[],int);
int main() {
long decimalNumber;
int b;
int sum=0;
printf("Enter a decimal number ");
scanf("%ld", &decimalNumber);
decimalToBinary(decimalNumber);
return 0;
}

/* Function to convert a decinal number to binary number */
void decimalToBinary(long n) {
int remainder;
int i = 1,j=0,size=0;
int a[32];
while(n != 0) {
remainder = n%2;
n = n/2;
a[j] = remainder;
j=j+1;
}
size=j;
int k=0;
int r[size];
for(int i=size-1;i>=0;i--)
{
r[k]=a[i];
printf("%d",r[k]);
}

numberAfterBitFlip(a,size);
}

void numberAfterBitFlip(int result[] ,int size)
{
int b;
printf(" Enter which bit you want to flip ");
scanf("%d",&b);
if(b >= 0 && b<=31)
{
for(int i=0;i<size;i++)
{
if(i==b)
{
if(result[i]==0)
{
result[i]=1;

}
else
{
result[i]=0;
}
}
}
}
int x=0,sum=0;
int s[32];
printf("after bit flipping ");
for(int i=size-1;i>=0;i--)
{
s[x]=result[i];
printf("%d",s[x]);
  
}
  
for(int i=0;i<size;i++)
{
  
sum = sum + result[i]*pow(2,i);
}
  
printf(" result is =%d",sum);
  
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT -   

Enter a decimal number 10
1010
Enter which bit you want to flip 2
after bit flipping 1110
result is =14

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