#include #include #include using namespace std; //char nibble_to_hex(uint8_t i) char nibble_to_hex(uint8_t i) //Conversion to hex format { assert(0x0 <= i && i <= 0xf); return "0123456789abcdef"[i]; } void print_in_hex(uint8_t byte) { cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0xf); } const uint8_t* as_bytes(const void* data); void print_in_hex(const void* data, size_t size) { const uint8_t* bytes = as_bytes(data); for (size_t i = 0; i < size; i++) { print_in_hex(bytes[i]); if ((i + 1) % 16 == 0) { cout << '\n'; } else { cout << ' '; } } } const uint8_t* as_bytes(const void* data) { return reinterpret_cast(data); } char bit_digit(uint8_t byte, uint8_t bit) { if (byte & (0x1 << bit)) { return '1'; } return '0'; } void print_in_binary(uint8_t byte) { for (int bit = 7; bit >= 0; bit--) { cout << bit_digit(byte, bit); } } void print_in_binary(const void* data, size_t size) { const uint8_t* bytes = static_cast(data); for (size_t i = 0; i < size; i++) { // Добавляем пробел перед байтом, кроме первого в строке if (i % 4 == 0 && i != 0) { cout << "\n"; } else if (i != 0) { cout << " "; } print_in_binary(bytes[i]); } cout << "\n"; // Перенос строки в конце } struct Example { uint8_t a; uint16_t b; uint32_t c; char d[3]; }; int main() { // Тестирование чисел из лекционного слайда uint16_t a = 0x4d2; // 1234 в десятичной uint16_t b = 0x1234; // 4660 в десятичной cout << "0x4d2 (1234) in binary:\n"; print_in_binary(&a, sizeof(a)); cout << "\n\n"; cout << "0x1234 (4660) in binary:\n"; print_in_binary(&b, sizeof(b)); cout << "\n\n"; // Пример с арифметическими операциями uint16_t sum = a + b; uint16_t diff = b - a; uint16_t xorr = a ^ b; cout << "Sum (0x4d2 + 0x1234):\n"; print_in_binary(&sum, sizeof(sum)); cout << "\n\n"; cout << "Difference (0x1234 - 0x4d2):\n"; print_in_binary(&diff, sizeof(diff)); cout << "\n\n"; cout << "XOR (0x4d2 ^ 0x1234):\n"; print_in_binary(&xorr, sizeof(xorr)); cout << "\n\n"; Example arr[2] = { {0x01, 0x0203, 0x04050607, {'a', 'b', 'c'}}, {0x08, 0x090a, 0x0b0c0d0e, {'d', 'e', 'f'}} }; cout << "The location of the structure in memory (hex):\n"; print_in_hex(arr, sizeof(arr)); cout << "\n\nThe location of the structure in memory (binary):\n"; print_in_binary(arr, sizeof(arr)); cin.ignore(); return 0; }