Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

60 строки
1.5 KiB
C++

#include <iostream>
#include <iomanip> // Для std::hex и std::bitset
#include <cstdint> // Для 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;
}