From 193cab7c1f98eb8d1008a7fbdb6d2c5c345cf2f0 Mon Sep 17 00:00:00 2001 From: NosovDI Date: Fri, 27 Oct 2023 14:45:03 +0700 Subject: [PATCH] add task2 --- task2.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 task2.cpp diff --git a/task2.cpp b/task2.cpp new file mode 100644 index 0000000..007ab8c --- /dev/null +++ b/task2.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +uint16_t reverse(uint16_t x) +{ + x = (x & 0xFF) << 8 | (x & 0xFF00) >> 8; + return x; +} + +void print_in_hex(uint16_t num) +{ + printf("%04X ", num); +} + +void print_in_binary(uint16_t num) +{ + for (int i = 15; i >= 0; i--) + { + printf("%d", (num >> i) & 1); + if (i % 8 == 0) + printf(" "); + } +} + +int main() +{ + setlocale(LC_ALL, "Russian"); + + uint16_t operand1, operand2; + char symbol; + + scanf("%hu %c %hu", &operand1, &symbol, &operand2); + + operand1 = reverse(operand1); + operand2 = reverse(operand2); + + uint16_t result; + switch (symbol) + { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + default: + printf("Неверный оператор\n"); + return 1; + } + + printf("\nРезультат: "); + print_in_hex(operand1); + printf("%c ", symbol); + print_in_hex(operand2); + printf("= "); + print_in_binary(operand1); + printf("%c ", symbol); + print_in_binary(operand2); + printf("= "); + print_in_binary(result); + printf("\n"); + + system("pause"); + return 0; +} \ No newline at end of file