Write a C program that takes two command line arguments. Both arguments are expe
ID: 3670491 • Letter: W
Question
Write a C program that takes two command line arguments. Both arguments are expected to be decimal numeric values and they are to be restricted to 8-bit unsigned integers. The program must be able to display each value in binary form. The first argument must serve as the mask to perform operations on the second argument. The program should display a menu that allows the user to select different bit-wise operations to be performed on the second argument using the given mask. The required operations are: Set, Clear and Toggle.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
unsigned Tobinary(unsigned int k) {
if (k == 0) return 0;
if (k == 1) return 1; /* optional */
return (k % 2) + 10 * Tobinary(k / 2);
}
int main(int argc,char **argv)
{
int choice;
unsigned int a=atoi(argv[1]);
unsigned int b=atoi(argv[2]);
printf(" 1.Set 2.clear 3.Toggle");
printf(" Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf(" Before setting the bit your value is");
printf("%d",Tobinary(a));
printf(" You are setting %d bit",b);
a!=1<<b;
printf(" After setting the bit your value is");
printf("%d",Tobinary(a!=1<<b));
break;
case 2:
printf(" Before clearing the bit your value is");
printf("%d",Tobinary(a));
printf(" You are clearing %d bit",b);
printf(" After clearing the bit your value is");
printf("%d",Tobinary(a &= ~(1 << b)));
break;
case 3:
printf(" Before toggle the bit your value is");
printf("%d",Tobinary(a));
printf(" You are Toggling %d bit",b);
printf(" After Toggling the bit your value is");
printf("%d",Tobinary(a ^= 1 << b));
break;
default:printf(" Enter correct choice....");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.