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

71 строка
1.7 KiB
C++

#include <iostream>
#include <iomanip>
#include <cstdint>
void print_in_hex(uint16_t value)
{
std::cout << std::setw(2) << std::setfill('0') << std::hex << (value >> 8) << " "
<< std::setw(2) << std::setfill('0') << std::hex << (value & 0xFF);
}
void print_in_binary(uint16_t value)
{
for (int i = 15; i >= 0; --i)
{
std::cout << ((value >> i) & 1);
if (i % 4 == 0) // Пробел после каждых 4 бит
std::cout << " ";
}
}
int main()
{
setlocale(0, "ru-ru");
uint16_t operand1, operand2;
char op;
std::cout << "Введите первый операнд: ";
std::cin >> operand1;
std::cout << "Введите оператор (&, |, ^): ";
std::cin >> op;
std::cout << "Введите второй операнд: ";
std::cin >> operand2;
uint16_t result;
switch (op) {
case '&':
result = operand1 & operand2;
break;
case '|':
result = operand1 | operand2;
break;
case '^':
result = operand1 ^ operand2;
break;
default:
std::cerr << "Неверный оператор!" << std::endl;
return 1;
}
std::cout << std::endl;
std::cout << "Шестнадцатеричное представление:" << std::endl;
print_in_hex(operand1);
std::cout << " " << op << " ";
print_in_hex(operand2);
std::cout << " = ";
print_in_hex(result);
std::cout << std::endl;
std::cout << "Двоичное представление:" << std::endl;
print_in_binary(operand1);
std::cout << " " << op << " ";
print_in_binary(operand2);
std::cout << " = ";
print_in_binary(result);
std::cout << std::endl;
return 0;
}