This is another C programming lab and therefore we will not meet in lab during t
ID: 3862713 • Letter: T
Question
This is another C programming lab and therefore we will not meet in lab during this week of the semester (the week after spring break). As you already know it is very easy but unwise to procrastinate on these C programs. Please start early and get help before the last minute. For this program, you should focus some attention on the user interface. Make your prompts and menus clear and formal. Write a program that takes three parameters from the command line at run time: First parameter must be a decimal number in the range of 0 - 65535 (16 bit unsigned) Second parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned) Third parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned) The program should completely validate the input parameters and report those errors before exiting back to the terminal. Assuming that input is properly validated: The first parameter should be stored in a 16 bit data type variable The second and third parameters should be stored in 8 bit data type variable The second parameter will be used as the mask for bits 15-8 of the first parameter. The third parameter will be used as the mask for bits 7-0 of the first parameter. The program should display all values (if no input error) in both decimal and binary format. The program should then display a menu for the user to choose from the following operations:
Set – Makes the bit a 1 regardless of its original value
Clear – Makes the bit a 0 regardless of its original value
Toggle – Makes the bit the opposite of its original value
Exit – return to the terminal After the user makes a choice, the program should perform the complete bitwise operation on the first parameter and display it in binary format (or exit). The program should then redisplay the menu and waits for another choice from the user. You will need to understand and use bitwise operators.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int decimalToBinary(int value, int bit, int a[]);
int main()
{
int choice;
int p1;
int p2;
int p3;
int i;
int a[16]={0};
int b[8]={0};
int c[8]={0};
int mask[16];
int set[16];
// takes three parameters from the command line at run time
//First parameter must be a decimal number in the range of 0 - 65535 (16 bit unsigned)
printf(" Enter First Parameter : ");
scanf("%d",&p1);
if(p1>0 && p1<65535 )
{
}
else
{
printf(" First parameter must be a decimal number in the range of 0 - 65535 (16 bit unsigned)") ;
return;
}
// Second parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned)
printf(" Enter Second Parameter : ");
scanf("%d",&p2);
if(p2>0 && p2<255)
{
}
else
{
printf(" Second parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned)") ;
return;
}
//Third parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned)
printf(" Enter Third Parameter : ");
scanf("%d",&p3);
if(p3>0 && p3<255)
{
}
else
{
printf(" Third parameter must be a decimal number in the range of 0 - 255 (8 bit unsigned)");
return;
}
decimalToBinary(p1,16,a);
printf(" Decimal Value %d and binary value is : ",p1);
for(i=0;i<16;i++)
printf("%d",a[i]);
decimalToBinary(p2,8,b);
printf(" Decimal Value %d and binary value is : ",p2);
for(i=0;i<8;i++)
printf("%d",b[i]);
decimalToBinary(p3, 8,c);
printf(" Decimal Value %d and binary value is : ",p3);
for(i=0;i<8;i++)
printf("%d",c[i]);
for(i=0;i<8;i++)
{
mask[i]=c[i];
}
for(i=0;i<8;i++)
{
mask[8+i]=b[i];
}
printf(" Mask is : ");
for(i=0;i<16;i++)
printf("%d",mask[i]);
printf(" ----------------Menu---------------- ");
printf(" 1.Set");
printf(" 2.Clear");
printf(" 3.Toggle");
printf(" 4.Exit");
printf(" Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: for(i=0;i<16;i++)//Set – Makes the bit a 1 regardless of its original value
set[i]=a[i]|mask[i];
printf(" Set := ");
for(i=0;i<16;i++)
printf("%d",set[i]);
break;
case 2: //Clear – Makes the bit a 0 regardless of its original value
for(i=0;i<16;i++)
set[i]=a[i]&mask[i];
printf(" Clear := ");
for(i=0;i<16;i++)
printf("%d",set[i]);
break;
case 3: //Toggle – Makes the bit the opposite of its original value
for(i=0;i<16;i++)
set[i]=a[i]^mask[i];
printf(" Toggle := ");
for(i=0;i<16;i++)
printf("%d",set[i]);
break;
case 4: return;
default: printf("You Entered Wrong choice");
}
return 0;
}
/* Function to convert a decinal number to binary number */
int decimalToBinary(int n, int bit, int d[])
{
while(n != 0)
{
d[--bit] = n%2;
n = n/2;
}
return d;
}
-----------------------------------------------------------
Output sample 1:-
Enter First Parameter : 234
Enter Second Parameter : 34
Enter Third Parameter : 23
Decimal Value 234 and binary value is : 0000000011101010
Decimal Value 34 and binary value is : 00100010
Decimal Value 23 and binary value is : 00010111
Mask is : 0001011100100010
----------------Menu----------------
1.Set
2.Clear
3.Toggle
4.Exit
Enter Your Choice : 1
Set := 0001011111101010
--------------------------------------------
Output sample 2:-
Enter First Parameter : 234
Enter Second Parameter : 43
Enter Third Parameter : 64
Decimal Value 234 and binary value is : 0000000011101010
Decimal Value 43 and binary value is : 00101011
Decimal Value 64 and binary value is : 01000000
Mask is : 0100000000101011
----------------Menu----------------
1.Set
2.Clear
3.Toggle
4.Exit
Enter Your Choice : 2
Clear := 0000000000101010
----------------------
Output sample 3:-
Enter First Parameter : 345
Enter Second Parameter : 34
Enter Third Parameter : 65
Decimal Value 345 and binary value is : 0000000101011001
Decimal Value 34 and binary value is : 00100010
Decimal Value 65 and binary value is : 01000001
Mask is : 0100000100100010
----------------Menu----------------
1.Set
2.Clear
3.Toggle
4.Exit
Enter Your Choice : 3
Toggle := 0100000001111011
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.