Битовый калькулятор
Этот коммит содержится в:
91
2.cpp
Обычный файл
91
2.cpp
Обычный файл
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
using namespace std;
|
||||
|
||||
// Функции для вывода значений в шестнадцатеричном и двоичном форматах
|
||||
void print_hex_byte(uint8_t byte) {
|
||||
static const char hex_chars[] = "0123456789abcdef";
|
||||
cout << hex_chars[byte >> 4] << hex_chars[byte & 0x0F];
|
||||
}
|
||||
|
||||
void print_hex_value(uint16_t value) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
|
||||
print_hex_byte(bytes[0]);
|
||||
cout << " ";
|
||||
print_hex_byte(bytes[1]);
|
||||
}
|
||||
|
||||
void print_binary_byte(uint8_t byte) {
|
||||
for (int i = 7; i >= 0; --i) {
|
||||
cout << ((byte >> i) & 1);
|
||||
}
|
||||
}
|
||||
|
||||
void print_binary_value(uint16_t value) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
|
||||
print_binary_byte(bytes[0]);
|
||||
cout << " ";
|
||||
print_binary_byte(bytes[1]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Установка русской локали
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
uint16_t operand1, operand2;
|
||||
char operation;
|
||||
|
||||
// Ввод первого операнда
|
||||
cout << "Введите первый операнд (uint16_t): ";
|
||||
cin >> operand1;
|
||||
|
||||
// Ввод оператора
|
||||
cout << "Введите оператор (&, |, ^): ";
|
||||
cin >> operation;
|
||||
|
||||
// Проверка корректности оператора
|
||||
if (operation != '&' && operation != '|' && operation != '^') {
|
||||
cout << "Ошибка: недопустимый оператор. Используйте &, |, ^." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Ввод второго операнда
|
||||
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;
|
||||
}
|
||||
|
||||
// Вывод результатов
|
||||
cout << "Результат в шестнадцатеричном формате:" << endl;
|
||||
print_hex_value(operand1);
|
||||
cout << " " << operation << " ";
|
||||
print_hex_value(operand2);
|
||||
cout << " = ";
|
||||
print_hex_value(result);
|
||||
cout << endl;
|
||||
|
||||
cout << "Результат в двоичном формате:" << endl;
|
||||
print_binary_value(operand1);
|
||||
cout << " " << operation << " ";
|
||||
print_binary_value(operand2);
|
||||
cout << " = ";
|
||||
print_binary_value(result);
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Ссылка в новой задаче
Block a user