Question

The Problem Write a C or C++ program which performs specific operations on bits. The user...

The Problem

Write a C or C++ program which performs specific operations on bits. The user will specify a value The user can then set, test, or toggle individual bits in the initial value.

Input

Your program will prompt the user for:

  • An inital value in hexadecimal. This value may be up to 32 bits (eight hexadecimal digits) long. If the user-supplied value is less than eight characters in length, assume the additional digits are zero and are on the left side. Values longer than eight characters should be rejected.
  • The desired operation: This can be the words "Test", "Set", "Toggle", or "End".
  • For the "Test", "Set", and "Toggle" operations, a bit number must be provided. This must be in the range of zero to 31.
  • For the "Set" operation, a new bit setting (0 or 1) must be provided.

If any any of the above values is incorrect, your program should prompt the user to re-enter the value until it is in the acceptable range.

Processing

Your program should perform the following steps:

  1. Display message 1.
  2. Display message 2 prompting the user for the input value.
  3. Input the hexadecimal value. If it is longer than eight characters, display message 3 and return to the prior step.
  4. Convert the hexadecimal character string to an unsigned int. If invalid hexadecimal characters are detected, display message 4 and return to step 2. Allow both upper and lower case characters.
  5. Display message 6.
  6. Input the user command. If it is not one of the valid commands, return to the prior step. Allow any combination of upper and lower case characters.
  7. If the command is 'End', stop execution.
  8. Issue message 7.
  9. Input the bit number. If it is not in the range of zero to 31, return to the prior step.
  10. Build a bit mask (an unsigned int which has only the specified bit set on).
  11. If the command is 'Set';
    1. Issue message 10, then input the new bit value.
    2. If the new bit value is not 0 or 1, return to the prior step.
    3. If the new bit value is 1, perform a bitwise or operation on the hexadecimal value versus the bit mask.
    4. If the new bit value is 0, perform a bitwise and operation on the hexadecimal value versus the inverted bit mask.
    5. Issue message 8 showing the new hexadecimal value.
  12. If the command is 'Test':
    1. Use a bitwise and operation to determine if the bit is on.
    2. Issue message 9 showing the result.
  13. If the command is 'Toggle':
    1. Perform a bitwise exclusive or operation on the hexadecimal value versus the bit mask.
    2. Issue message 8 showing the new hexadecimal value.

Output

Message 1:


Pierce College CS516 Fall 2019 Lab Assignment 1 - Last, first

Replace Last, first with your name.

Message 2:


Enter the hexadecimal value, eight digits maximum

Message 3:


Value user's value is too long -- re-enter

Replace user's value with the value the user provided.

Message 4:


Digit digit is not valid, must be 0-9 or A-F, re-enter

Replace digit with the incorrect digit.

Message 5:

This message is omitted for this version of the assignment.

Message 6:


Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish

Message 7:


Enter bit number (0 to 31)

Message 8:


Value changed to XXXXXXXX

Replace XXXXXXXX with the new hexadecimal value.

Message 9:


Bit NN is binary value

Replace NN with the user-specified bit number, and binary value with 0 or 1.

Message 10:


Enter new bit value (0 or 1)

Sample Execution


Pierce College CS516 Fall 2019 Lab Assignment 1 - Murphy, J.H.

Enter the hexadecimal value, eight digits maximum

ABCDEF012

Value ABCDEF012 is too long -- re-enter

ABCDEG01

Digit G is not valid, must be 0-9 or A-Z, re-enter

ABCDEF01

Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish

Test

Enter bit number (0 to 31)

0

Bit 0 is 1

Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish

Set

Enter bit number (0 to 31)

3

Enter new Bit value

1

Value changed to ABCDEF09

Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish


End

Homework Answers

Answer #1

Screenshot

Program

//Header files
#include <iostream>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;

//Main method
int main()
{
   //Variable for input
   string hexVal;
   //Message1
   cout << "Pierce College CS516 Fall 2019 Lab Assignment 1 - Last, first\nReplace Last, first with your name." << endl;
   //Message 2
   cout << "Enter the hexadecimal value, eight digits maximum" << endl;
   cin >> hexVal;
   //Error check in input
   while (true) {
       bool flag = false;
       //Length check
       while (hexVal.length() > 8) {
           cout << "Value " << hexVal << " is too long -- re-enter" << endl;
           cin >> hexVal;
       }
       //Hex check
       for (int i = 0; i < hexVal.length(); i++) {
           if (!isdigit(hexVal[i]) && hexVal[i] != 'A' && hexVal[i] != 'B' && hexVal[i] != 'C' && hexVal[i] != 'D' && hexVal[i] != 'E' && hexVal[i] != 'F') {
               cout << "Digit " << hexVal[i] << " is not valid, must be 0-9 or A-Z, re-enter" << endl;
               flag = true;
               break;
           }
       }
       if (flag == false) {
           break;
       }
       cin >> hexVal;
   }
   //Convert into bit
   stringstream hexStream;
   hexStream << hex << hexVal;
   unsigned bits;
   hexStream >> bits;
   bitset<32> binHex(bits);
    //User prompt starts here
   string cmd;
   int bit,val;
   //Prompt for command
   cout << "Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish" << endl;
   cin >> cmd;
   //Loop unti user enter End
   while (cmd != "End") {
       //Test, return corresponding bit value
       if (cmd == "Test"){
           cout << "Enter bit number (0 to 31)" << endl;
           cin >> bit;
           cout << "Bit 0 is " << binHex[bit] << endl;
       }
       //set, change a bit
       else if (cmd == "Set") {
           cout << "Enter bit number (0 to 31)" << endl;
           cin >> bit;
           cout << "Enter new Bit value" << endl;
           cin >> val;
           binHex[bit] = val;
           stringstream result;
           result << hex << uppercase << binHex.to_ulong();
           cout << "Value changed to " << result.str() << endl;
       }
       //Toggle, change bit from 0 to 1 or 1 to 0
       else if (cmd == "Toggle") {
           cout << "Enter bit number (0 to 31)" << endl;
           cin >> bit;
           if (binHex[bit] == 0) {
               binHex[bit] = 1;
           }
           else {
               binHex[bit] = 0;
           }
           stringstream result;
           result << hex << uppercase << binHex.to_ulong();
           cout <<"Value changed to "<<result.str() << endl;
       }
       //Repetition part
       cout << "Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish" << endl;
       cin >> cmd;
   }
}

Output

Pierce College CS516 Fall 2019 Lab Assignment 1 - Last, first
Replace Last, first with your name.
Enter the hexadecimal value, eight digits maximum
ABCDEF012
Value ABCDEF012 is too long -- re-enter
ABCDEG01
Digit G is not valid, must be 0-9 or A-Z, re-enter
ABCDEF01
Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish
Test
Enter bit number (0 to 31)
0
Bit 0 is 1
Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish
Set
Enter bit number (0 to 31)
3
Enter new Bit value
1
Value changed to ABCDEF09
Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish
Toggle
Enter bit number (0 to 31)
3
Value changed to ABCDEF01
Enter 'Set' to change bits, 'Test' to check bit values, 'Toggle' to reverse a bit, or 'End' to finish
End

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the...
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the hyperfactorial is equivalent to value obtained by the operation: 1^1*2^2*3^3*…..n^n . If user inputs the value that is not a positive value, display a message informing the user that the input is not valid and request a new input. Calculate the hyperfactorial first by creating a program that uses only nested “For loop” structure.
(In c code only, not c++) We will simulate the status of 8 LEDs that are...
(In c code only, not c++) We will simulate the status of 8 LEDs that are connected to a microcontroller. Assume that the state of each LED (ON or OFF) is determined by each of the bits (1 or 0) in an 8-bit register (high-speed memory). Declare a char variable called led_reg and initialize it to 0. Assume that the least-significant bit (lsb) controls LED#0 and the most-significant bit (msb) controls LED#7. In the main function, build and present a...
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write a program (C language) that will read the number of a month and will print...
Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February. Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program. Program must meet the following criteria: 1.Your name and the name of the program...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double. Your program should do some basic exception handling: (1) If the file cannot be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT