Write a MENU DRIVEN program in c language to 1) Print the binary conversion of a
ID: 3590642 • Letter: W
Question
Write a MENU DRIVEN program in c language to
1) Print the binary conversion of a number
2) Convert a target bit to 1 of the number passed
3) Extract the specific byte from a given number
4) Generate the new number as mentioned
0) Exit the program
The program should start with a user prompt to enter an integer number.
Option A will print the binary equivalent of the decimal number entered.
Prototype : void printBinary(int);
Option B will set the target bit of an integer to 1, and returns the new value. For example, when integers x and y are given to the function, the yth bit (from the right side ) in x should be set to 1. Note that y should be between 0 and 31 inclusively.
Prototype : int convertBit(int, int );
(Call printBinary with the returned number to validate)
Option C extracts a certain byte (1 to 4 from right to left) from a given integer and returns the extracted byte as an integer. For example, when two integers x and 2 are passed to the function, the function returns the 2nd byte from x.
Prototype : int extractByte(int, int );
Option D creates a new number with a certain number of bits to 1 and all the other bits to 0. The value should be returned from the function. For example, when integers x = 3 (3rd from right) and y = 8 (number of 1’s) are passed to the function, the function should return the integer that has a binary number of 00000000 00000000 00000111 11111000.
Prototype : int generateNumber(int, int );
Explanation / Answer
#include<stdio.h>
#include<math.h>
int main()
{
int ch;
int num,bit;
/* prompting the user for menu*
printf("Enter your choice ");
printf("%s ","1.Print the binary of a number");
printf("%s ","2.convert the target bit to 1 of number");
printf("%s ","3.Extract a specific Byte of the number");
printf("%s ","4.Generate a new number");
printf("%s ","0.Exit");
/* scanning the user choice */
scanf("%d",&ch);
do
{
/* evaluating based on the user choice*/
switch(ch)
{
case 1: printf("Enter number ");
scanf("%d",&num);
printBinary(num);
break;
case 2: printf(" Enter number ");
scanf("%d",&num);
printf(" Enter target bit ");
scanf("%d",&bit);
convertBit(num,bit);
break;
case 3: printf(" Enter the number");
scanf("%d",&num);
printf(" Enter the byte number");
scanf("%d",&bit);
extractByte(num,bit);
break;
case 4:printf(" Enter the position from which 1 ");
scanf("%d",&num);
printf(" Enter the number of 1's")
scanf("%d",&bit);
generateNumber(num,bit);
break;
case 0: return 0;
}
/* prompting the user for menu*
printf("Enter your choice ");
printf("%s ","1.Print the binary of a number");
printf("%s ","2.convert the target bit to 1 of number");
printf("%s ","3.Extract a specific Byte of the number");
printf("%s ","4.Generate a new number");
printf("%s ","0.Exit");
/* scanning the user choice */
scanf("%d",&ch);
}while(ch!=0);/* breaking loop if user choice is exit */
return 0;
}
/* prints binary number */
void printBinary(int num)
{
int n= num;int binary[32] = {0};
int i=0;
printf(" The binary number is : ")
/* stores the binary num in an array*/
while(n>=1)
{
binary[i] = n%2
n = n/2;
i++;
}
/* priting */
for(i=31;i>=0;i--)
printf("%d",binary[i])
printf(" ");
}
/*converts a bit of binary number */
int convertBit( int num, int bit )
{
int n = num,i=0;
int b[32] = {0};
/* getting binary number */
while(n>1)
{
b[i] = n%2;
n = n/2;
i++;
}
/* converting the specified bit from right to 1*/
b[bit-1] = 1;
/* calculating the value */
for(i =31;i>=0;i--)
{
n = b[i]*pow(2,i);
}
return n;
}
/* extracts a byte of binary numbe value */
int extractByte(int num, int byte)
{
int binary[32],b[4];
int n =num,i=0;
while(i<32)
{
binary[i] = n%2;
n =n /2;
i++;
}
/* extracting four bit values from specified byte */
b[0] = binary [4*(byte-1)];
b[1] = binary [4*(byte-1)+1];
b[2] = binary [4*(byte-1)+2];
b[3] = binary [4*(byte-1)+3];
/* returning its value*/
return (b[0]*1 + b[1]* 2 + b[2]*4 + b[3]*8);
}
/* generating a number that is with the specified number of 1's from specified position*/
int generateNumber(int num,int bit)
{
int binary[31] = {0};
int i=0,num=0;
while(i<(bit-1))
i++;
/* settign 1's in the binary number*/
while(i<(num+bit-1))
{
binary[i] = 1;
i++;
}
for(i=0;i<31;i++)
{
num = num + binary[i]*pow(2,i);
}
return num;
}
/* please include any dependencies if needed*/
/* any queries leave a comment. Thank you ! */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.