From d9e0562145941eaa50cba40811891bc101be573d Mon Sep 17 00:00:00 2001 From: TimofeevAnM Date: Sun, 26 Jan 2025 12:50:26 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB(=D0=B0)=20?= =?UTF-8?q?'lab04=5F2.cpp'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab04_2.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lab04_2.cpp diff --git a/lab04_2.cpp b/lab04_2.cpp new file mode 100644 index 0000000..d297d38 --- /dev/null +++ b/lab04_2.cpp @@ -0,0 +1,60 @@ +#include +#include // Для std::hex и std::bitset +#include // Для uint16_t + +using namespace std; + +// Функция для печати числа в шестнадцатеричном виде +void print_in_hex(uint16_t value) { + cout << "Hex: " << hex << setw(4) << setfill('0') << value << endl; +} + +// Функция для печати числа в двоичном виде +void print_in_binary(uint16_t value) { + cout << "Binary: "; + for (int i = 15; i >= 0; --i) { + cout << ((value >> i) & 1); + } + if (i == 8) { + cout << ' '; +} + + cout << endl; +} + +int main() { + uint16_t operand1, operand2; + char operation; + + // Ввод операндов и операции + cout << "Enter first operand (uint16_t): "; + cin >> operand1; + cout << "Enter operation (&, |, ^): "; + cin >> operation; + cout << "Enter second operand (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 << "Invalid operation!" << endl; + return 1; // Завершение программы с ошибкой + } + + // Вывод результатов + print_in_hex(result); + print_in_binary(result); + + return 0; +} \ No newline at end of file