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:
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:
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
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
Get Answers For Free
Most questions answered within 1 hours.