PvlukhinaYA 6 месяцев назад
Родитель 770a4d553b
Сommit 369a9e2020

@ -0,0 +1,74 @@
#include <iostream>
#include <cstdint>
#include <iomanip>
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;
}
Загрузка…
Отмена
Сохранить