From 5c1b621c9018926b747f7aabde919836e029c182 Mon Sep 17 00:00:00 2001 From: NikolaichukAlV Date: Wed, 15 Jan 2025 18:56:04 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=202.?= =?UTF-8?q?=20=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D1=82=D1=8C=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D1=83-=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=B1=D0=B8=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab04_task2.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 lab04_task2.cpp diff --git a/lab04_task2.cpp b/lab04_task2.cpp new file mode 100644 index 0000000..c406f2d --- /dev/null +++ b/lab04_task2.cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + +char nibble_to_hex(uint8_t i) { + static const char digits[] = "0123456789ABCDEF"; + return digits[i]; +} + +void print_byte_hex(uint8_t byte) { + cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F); +} + +void print_hex(uint16_t value) { + uint8_t* bytes = (uint8_t*)&value; + print_byte_hex(bytes[0]); + cout << " "; + print_byte_hex(bytes[1]); +} + +void print_byte_binary(uint8_t byte) { + for (int i = 7; i >= 0; i--) { + cout << ((byte >> i) & 1); + } +} + +void print_binary(uint16_t value) { + uint8_t* bytes = (uint8_t*)&value; + print_byte_binary(bytes[0]); + print_byte_binary(bytes[1]); +} + +int main() { + uint16_t operand1, operand2; + char operation; + + // Ввод данных + cin >> operand1 >> operation >> operand2; + + // Вычисление результата + uint16_t result; + switch (operation) { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + default: + cout << "Ошибка: поддерживаются только операторы &, | и ^" << endl; + return 1; + } + + // Вывод исходного выражения + cout << operand1 << " " << operation << " " << operand2 << endl; + + // Вывод в шестнадцатеричном виде + print_hex(operand1); + cout << " " << operation << " "; + print_hex(operand2); + cout << " = "; + print_hex(result); + cout << endl; + + // Вывод в двоичном виде + print_binary(operand1); + cout << " " << operation << endl; + print_binary(operand2); + cout << " =" << endl; + print_binary(result); + cout << endl; + + return 0; +}