diff --git a/lab04/main.cpp b/lab04/main.cpp index c91d6dc..34d1b6d 100644 --- a/lab04/main.cpp +++ b/lab04/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; @@ -80,6 +81,19 @@ struct Example { char d[3]; }; + +void print_in_hex(uint16_t value) { + uint8_t* bytes = reinterpret_cast(&value); + for (int i = 1; i >= 0; i--) { + cout << nibble_to_hex(bytes[i] >> 4) << nibble_to_hex(bytes[i] & 0xf) << " "; + } +} + +void print_in_binary(uint16_t value) { + bitset<16> bits(value); + cout << bits; +} + int main() { // Тестирование чисел из лекционного слайда uint16_t a = 0x4d2; // 1234 в десятичной @@ -120,7 +134,45 @@ int main() { cout << "\n\nThe location of the structure in memory (binary):\n"; print_in_binary(arr, sizeof(arr)); + cout << endl; + + char op; + + cout << "Enter first operand (hex): "; + cin >> hex >> a; + cout << "Enter operator (&, |, ^): "; + cin >> op; + cout << "Enter second operator (hex): "; + cin >> hex >> b; + + uint16_t result; + switch (op) { + case '&': result = a & b; break; + case '|': result = a | b; break; + case '^': result = a ^ b; break; + default: + cerr << "Operator incorrect!"; + return 1; + } - cin.ignore(); + // Вывод в hex + cout << "\nResult (hex): "; + print_in_hex(a); + cout << " " << op << " "; + print_in_hex(b); + cout << " = "; + print_in_hex(result); + + // Вывод в binary + cout << "\nResult (binary):\n"; + print_in_binary(a); + cout << " " << op << "\n"; + print_in_binary(b); + cout << " =\n"; + print_in_binary(result); + + cout << endl; + + system("pause"); return 0; }