From 369a9e20208c6699107b65ae5b4d46999f8a3b56 Mon Sep 17 00:00:00 2001 From: PvlukhinaYA Date: Wed, 25 Dec 2024 23:03:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=5F2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab04_2.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lab04_2.cpp diff --git a/lab04_2.cpp b/lab04_2.cpp new file mode 100644 index 0000000..cde073b --- /dev/null +++ b/lab04_2.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; + +// Преобразование значения в шестнадцатеричное представление +void print_in_hex(uint16_t value) +{ + cout << setw(4) << setfill('0') << hex << value; +} + +// Преобразование значения в двоичное представление +void print_in_binary(uint16_t value) +{ + for (int bit = 15; bit >= 0; --bit) + { + cout << ((value & (1 << bit)) ? '1' : '0'); + } +} + +int main() +{ + setlocale(LC_ALL, "Russian"); + uint16_t operand1, operand2; + char operation; + + // Запрос ввода операндов и операции + cout << "Введите первый операнд (uint16_t): "; + cin >> operand1; + cout << "Введите оператор (&, |, ^): "; + cin >> operation; + cout << "Введите второй операнд (uint16_t): "; + cin >> 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 << hex << "Hex: "; + print_in_hex(operand1); + cout << " " << operation << " "; + print_in_hex(operand2); + cout << " = "; + print_in_hex(result); + cout << endl; + + cout << "Binary: "; + print_in_binary(operand1); + cout << " " << operation << " "; + print_in_binary(operand2); + cout << " = "; + print_in_binary(result); + cout << endl; + + return 0; +} \ No newline at end of file