Родитель
e3ba03fc91
Сommit
5c1b621c90
@ -0,0 +1,77 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
char nibble_to_hex(uint8_t i) {
|
||||||
|
static const char digits[] = "0123456789ABCDEF";
|
||||||
|
return digits[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_byte_hex(uint8_t byte) {
|
||||||
|
cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_hex(uint16_t value) {
|
||||||
|
uint8_t* bytes = (uint8_t*)&value;
|
||||||
|
print_byte_hex(bytes[0]);
|
||||||
|
cout << " ";
|
||||||
|
print_byte_hex(bytes[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_byte_binary(uint8_t byte) {
|
||||||
|
for (int i = 7; i >= 0; i--) {
|
||||||
|
cout << ((byte >> i) & 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_binary(uint16_t value) {
|
||||||
|
uint8_t* bytes = (uint8_t*)&value;
|
||||||
|
print_byte_binary(bytes[0]);
|
||||||
|
print_byte_binary(bytes[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
uint16_t operand1, operand2;
|
||||||
|
char operation;
|
||||||
|
|
||||||
|
// Ââîä äàííûõ
|
||||||
|
cin >> operand1 >> operation >> 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 << operand1 << " " << operation << " " << operand2 << endl;
|
||||||
|
|
||||||
|
// Âûâîä â øåñòíàäöàòåðè÷íîì âèäå
|
||||||
|
print_hex(operand1);
|
||||||
|
cout << " " << operation << " ";
|
||||||
|
print_hex(operand2);
|
||||||
|
cout << " = ";
|
||||||
|
print_hex(result);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
// Âûâîä â äâîè÷íîì âèäå
|
||||||
|
print_binary(operand1);
|
||||||
|
cout << " " << operation << endl;
|
||||||
|
print_binary(operand2);
|
||||||
|
cout << " =" << endl;
|
||||||
|
print_binary(result);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче