Write a program in C language that lets a user manipulate bits individually in a
ID: 3784989 • Letter: W
Question
Write a program in C language that lets a user manipulate bits individually in a 4-byte variable. The program should begin with all bits having a value of zero. The program should enter a loop where it prints out the current bit values as a single integer using the twos complement bit model. It should then prompt the user to either set a bit, clear a bit, or exit. If the user desires to set a bit or clear a bit, then the programshould prompt the user for which bit, change the appropriate value, and then cycle back to the beginning of the loop. Setting a bit changes its value to 1 regardless of its current value; clearing a bit changes its value to 0 regardless of its current value.
Convert each of the following bit patterns into whole numbers. Assume the values are stored using the twos complement bit model 0010 1101 01011010 10010001 11100011 00101000 10110110 0110111100 101011 1100101111001000 1000000010100011Explanation / Answer
C program for manipulate bits individually in a 4-byte variable:
#include<stdio.h>
int main()
{
int number = 0,i;
int bit_position = 0;
int choice;
while(1)
{
for(i=31; i>=0; i--) {
printf("%i",((number & (1 << i))?1:0));
}
printf(" Enter 1. to set bit :");
printf(" Enter 2. to clear bit :");
printf(" Enter 3. to exit :");
printf(" Enter your choice :");
scanf("%d",&choice);
if(choice == 3) break;
if(choice ==1 || choice == 2)
{
printf(" Enter bit position to set or clear bit :");
scanf("%d", &bit_position);
}
if(choice == 1)
number |= 1 << bit_position;
if(choice == 2)
number &= ~(1 << bit_position);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.