main
IvanchenkoIS 5 месяцев назад
Родитель 02b31d6fe2
Сommit 046f3be8f9

81
2.cpp

@ -2,76 +2,71 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
char nibble_to_hex(uint8_t i) { char hex_digit(uint8_t value) {
static const char digits[] = "0123456789ABCDEF"; static const char hex_chars[] = "0123456789ABCDEF";
return digits[i]; return hex_chars[value];
} }
void print_byte_hex(uint8_t byte) { void display_hex_byte(uint8_t byte) {
cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F); cout << hex_digit(byte >> 4) << hex_digit(byte & 0x0F);
} }
void print_hex(uint16_t value) { void display_hex_value(uint16_t data) {
uint8_t* bytes = (uint8_t*)&value; uint8_t* bytes = reinterpret_cast<uint8_t*>(&data);
print_byte_hex(bytes[0]); display_hex_byte(bytes[0]);
cout << " "; cout << " ";
print_byte_hex(bytes[1]); display_hex_byte(bytes[1]);
} }
void print_byte_binary(uint8_t byte) { void display_binary_byte(uint8_t byte) {
for (int i = 7; i >= 0; i--) { for (int pos = 7; pos >= 0; --pos) {
cout << ((byte >> i) & 1); cout << ((byte >> pos) & 1);
} }
} }
void print_binary(uint16_t value) { void display_binary_value(uint16_t data) {
uint8_t* bytes = (uint8_t*)&value; uint8_t* bytes = reinterpret_cast<uint8_t*>(&data);
print_byte_binary(bytes[0]); display_binary_byte(bytes[0]);
print_byte_binary(bytes[1]); display_binary_byte(bytes[1]);
} }
int main() { int main() {
uint16_t operand1, operand2; uint16_t num1, num2;
char operation; char op;
// Ввод данных cin >> num1 >> op >> num2;
cin >> operand1 >> operation >> operand2;
// Вычисление результата
uint16_t result; uint16_t result;
switch (operation) { switch (op) {
case '&': case '&':
result = operand1 & operand2; result = num1 & num2;
break; break;
case '|': case '|':
result = operand1 | operand2; result = num1 | num2;
break; break;
case '^': case '^':
result = operand1 ^ operand2; result = num1 ^ num2;
break; break;
default: default:
cout << "Ошибка: поддерживаются только операторы &, | и ^" << endl; cout << "Ошибка: доступны только &, | и ^" << endl;
return 1; return 1;
} }
// Вывод исходного выражения cout << num1 << " " << op << " " << num2 << endl;
cout << operand1 << " " << operation << " " << operand2 << endl;
// Вывод в шестнадцатеричном виде display_hex_value(num1);
print_hex(operand1); cout << " " << op << " ";
cout << " " << operation << " "; display_hex_value(num2);
print_hex(operand2);
cout << " = "; cout << " = ";
print_hex(result); display_hex_value(result);
cout << endl; cout << endl;
// Вывод в двоичном виде display_binary_value(num1);
print_binary(operand1); cout << " " << op << endl;
cout << " " << operation << endl; display_binary_value(num2);
print_binary(operand2);
cout << " =" << endl; cout << " =" << endl;
print_binary(result); display_binary_value(result);
cout << endl; cout << endl;
return 0; return 0;
} }

Загрузка…
Отмена
Сохранить