Added a bitwise calculator
Этот коммит содержится в:
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <bitset>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -80,6 +81,19 @@ struct Example {
|
||||
char d[3];
|
||||
};
|
||||
|
||||
|
||||
void print_in_hex(uint16_t value) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
|
||||
for (int i = 1; i >= 0; i--) {
|
||||
cout << nibble_to_hex(bytes[i] >> 4) << nibble_to_hex(bytes[i] & 0xf) << " ";
|
||||
}
|
||||
}
|
||||
|
||||
void print_in_binary(uint16_t value) {
|
||||
bitset<16> bits(value);
|
||||
cout << bits;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Òåñòèðîâàíèå ÷èñåë èç ëåêöèîííîãî ñëàéäà
|
||||
uint16_t a = 0x4d2; // 1234 â äåñÿòè÷íîé
|
||||
@@ -120,7 +134,45 @@ int main() {
|
||||
cout << "\n\nThe location of the structure in memory (binary):\n";
|
||||
print_in_binary(arr, sizeof(arr));
|
||||
|
||||
cout << endl;
|
||||
|
||||
cin.ignore();
|
||||
char op;
|
||||
|
||||
cout << "Enter first operand (hex): ";
|
||||
cin >> hex >> a;
|
||||
cout << "Enter operator (&, |, ^): ";
|
||||
cin >> op;
|
||||
cout << "Enter second operator (hex): ";
|
||||
cin >> hex >> b;
|
||||
|
||||
uint16_t result;
|
||||
switch (op) {
|
||||
case '&': result = a & b; break;
|
||||
case '|': result = a | b; break;
|
||||
case '^': result = a ^ b; break;
|
||||
default:
|
||||
cerr << "Operator incorrect!";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Âûâîä â hex
|
||||
cout << "\nResult (hex): ";
|
||||
print_in_hex(a);
|
||||
cout << " " << op << " ";
|
||||
print_in_hex(b);
|
||||
cout << " = ";
|
||||
print_in_hex(result);
|
||||
|
||||
// Âûâîä â binary
|
||||
cout << "\nResult (binary):\n";
|
||||
print_in_binary(a);
|
||||
cout << " " << op << "\n";
|
||||
print_in_binary(b);
|
||||
cout << " =\n";
|
||||
print_in_binary(result);
|
||||
|
||||
cout << endl;
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user