Question

In: Computer Science

Write a C program to run on ocelot which will either set or clear a single...

Write a C program to run on ocelot which will either set or clear 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 inclusive using a scanf. Use an unsigned integer type. Output is to the screen. You must use only bitwise operators for this program. You can shift bits and or use the logical bitwise operators.

For this assignment give the user directions asking them to enter the integer and then ask the user if he or she wants to clear or set a bit. Then ask the user which bit to set or clear. That can be a number between 0 and 31. Prompt the user for what should be entered each time. Be sure to validate all user input so the program cannot be crashed. After completing the operation ask the user if they want to do the entire operation again. The user would enter Y or y to do it again. This would be the entire operation from entering the first integer.

You do not need to use getopt for this program since there are no command line arguments.

  • If the user enters a 1 as the initial value and a 0 for the bit to clear the result would be 0.

  • If the user enters a 1 as the initial value and a 0 for the bit to set the result would be 1.

  • If the user enters a 10 as the initial value and a 1 for the bit to clear then the result would be 8.

  • If the user enters a 10 as the initial value and a 1 for the bit to set then the result would be 10.

  • If the user enters a 10 as the initial value and a 2 for the bit to clear then the result would be 10.

  • If the user enters a 10 as the initial value and a 2 for the bit to set then the result would be 14.

    Output for each operation should be easy to read giving the number before the switch and then the decimal number after the switch. No other output should be included.

    The program should compile to create an executable called bitops.

  • Test the program with the following commands: o bitops

    ▪ enter 10 as the initial value and a 2 for the bit to clear o bitops

    ▪ enter 10 as the initial value and a 1 for the bit to set

Solutions

Expert Solution

Code to copy:

#include <stdio.h>

int main()

{
//declare the variables
int num, bit, res;
int option;
char ch = 'Y';
//iterate a do-while loop
do
{
/*Read an integer value between 1 and 1000
Validate the num. Iterate the loop until user enter
correct num.
*/
do
{
printf("Please enter an integer between 1 and 1000: ");
scanf("%d", & num);
if (num < 1 || num > 1000)
{
printf("Invalid Input.\n");
}
} while (num < 1 || num > 1000);
/*prompt and read the num if user wants to clear or set a bit
Validate the num. Iterate the loop until user enter
correct num.
*/
do
{
printf("\nDo you want to set or clear?\n1. Set\n2. Clear\nEnter your option: ");
scanf("%d", & option);
if (option < 1 || option > 2)
{
printf("Invalid Invalid.\n");
}
} while (option < 1 || option > 2);
//if the user wants to set the bit.
if (option == 1)
{
/*prompt and read the bit number to set
Validate the num. Iterate the loop until user enter
correct num.
*/
do
{
printf("\nEnter the bit to set (0-31): ");
scanf("%d", & bit);
if (bit < 0 || bit > 31)
{
printf("Invalid Invalid.\n");
}
} while (bit < 0 || bit > 31);
//using logical bitwise operators
//set the bit of the num number.
//Left shift 1 to bit number of times and perform bitwise OR (|)with num
res = (1 << bit) | num;
printf("The decimal number before setting %d bit: %d\n", bit, num);
printf("The decimal number after setting %d bit: %d\n", bit, res);
}
//if the user wants to clear the bit.
if (option == 2)
{
/*prompt and read the bit number to set
Validate the num. Iterate the loop until user enter
correct num.
*/
do
{
printf("\nEnter the bit to clear (0-31): ");
scanf("%d", & bit);
if (bit < 0 || bit > 31)
{
printf("Invalid Input.\n");
}
} while (bit < 0 || bit > 31);
//using logical bitwise operators
//clear the bit of the num number.
//Left shift 1 to bit number of times, apply the compliment (~),
//and perform bitwise AND (&) with num.
res = num & (~(1 << bit));
printf("The decimal number before clearing %d bit: %d\n", bit, num);
printf("The decimal number after clearing %d bit: %d\n", bit, res);
}
//ask the user to repeat this process again
printf("\nIf you want to repeat, press Y otherwise N: ");
scanf(" %c", & ch);
printf("\n");
} while (ch == 'Y' || ch == 'y');
return 0;

}

Code Screenshot:

Output Screenshot:


Related Solutions

Write a working C++ program to ask the user for a set of grades which are...
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array....
Write an assembly language program code to clear and set bit 7th and 19th in a...
Write an assembly language program code to clear and set bit 7th and 19th in a 32-bit variable called N.
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
In C++, Write a program that calculates pay for either an hourly paid worker or a...
In C++, Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data: Hourly Paid: HoursWorked HourlyRate Salaried: Salary Bonus The program should also declare a union with two members. Each member should be a structure...
Create a schematic of a circuit using and write C program to run on a PIC...
Create a schematic of a circuit using and write C program to run on a PIC 16F88 that will flash between a red and green LED at 5Hz with the green on 75% of the time and the red on 25% of the time.
Show that the Canter set C has measure equal to zero. Please write a clear detailed...
Show that the Canter set C has measure equal to zero. Please write a clear detailed proof.
Using C++ Write One one single program with two or more functioncallsWrite a C++...
Using C++ Write One one single program with two or more function callsWrite a C++ function, smallest Index, that takes as parameters an int array and its size and returns the index of the smallest element in the array. Also the program should test the function.Write another function that prompts the user to input a string and outputs the string in uppercase letters. You must use a character array to store the string.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Write this program in a C++ language Calculate the ideal age of a spouse. Enter either...
Write this program in a C++ language Calculate the ideal age of a spouse. Enter either m or f from the keyboard in lower case. You may use string data for the gender. Convert the gender to upper case Enter an age from the keyboard, probably an integer You will need prompts telling the user what to enter. Use an IF statement to determine if the person is a male or female. You do one calculation if the person is...
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT