Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

205 строки
6.0 KiB
C++

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstdint>
#include <cstddef>
#include <locale>
using namespace std;
char nibble_to_hex(uint8_t i)
{
assert(0x0 <= i && i <= 0xf);
const char digits[] = "0123456789abcdef";
return digits[i];
}
uint8_t get_lower_nibble(uint8_t byte)
{
return byte & 0x0F;
}
uint8_t get_upper_nibble(uint8_t byte)
{
return byte >> 4;
}
void print_in_hex(uint8_t byte)
{
cout << nibble_to_hex(get_upper_nibble(byte))
<< nibble_to_hex(get_lower_nibble(byte));
}
const uint8_t* as_bytes(const void* data)
{
return reinterpret_cast<const uint8_t*>(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 << ' ';
}
}
}
void print_in_binary(uint8_t byte)
{
for (int bit = 7; bit >= 0; --bit)
{
cout << ((byte & (1 << bit)) ? '1' : '0');
}
}
void print_in_binary(const void* data, size_t size)
{
const uint8_t* bytes = as_bytes(data);
for (size_t i = 0; i < size; i++)
{
print_in_binary(bytes[i]);
if ((i + 1) % 4 == 0)
{
cout << '\n';
}
else
{
cout << ' ';
}
}
}
struct Student
{
char name[17];
uint16_t admissionYear;
float averageGrade;
uint8_t gender : 1;
uint32_t coursesCount;
Student* groupLeader;
};
int main()
{
setlocale(LC_ALL, "Russian");
for (uint8_t i = 0; i < 16; i++)
{
assert(nibble_to_hex(i) == (i < 10 ? '0' + i : 'a' + (i - 10)));
}
uint8_t byte = 0xab;
cout << "Hex: ";
print_in_hex(byte);
cout << '\n';
cout << "Binary: ";
print_in_binary(byte);
cout << '\n';
Student students[3] =
{
{"Alice Johnson", 2022, 3.5, 0, 4, nullptr},
{"John Smith", 2021, 3.8, 1, 5, nullptr},
{"", 0, 0.0, 0, 0, nullptr}
};
cout << "\n1) Адрес и размер массива: " << endl;
cout << "Адрес массива: " << static_cast<void*>(students) << endl;
cout << "Размер массива: " << sizeof(students) << " байт" << endl;
for (int i = 0; i < 3; i++)
{
cout << "2) Адреса и размеры элементов массива (Student[" << i << "]): " << endl;
cout << "Адрес: " << static_cast<void*>(&students[i]) << endl;
cout << "Размер: " << sizeof(students[i]) << " байт" << endl;
}
cout << "\n3) Сведения о полях одного элемента (Student[0]): " << endl;
Student& alice = students[0];
cout << "Имя:" << endl;
cout << "Адрес: " << static_cast<void*>(alice.name) << endl;
cout << "Смещение: " << offsetof(Student, name) << " байт" << endl;
cout << "Размер: " << sizeof(alice.name) << " байт" << endl;
cout << "Шестнадцатеричное: ";
for (size_t i = 0; i < sizeof(alice.name); i++) {
cout << hex << (static_cast<int>(alice.name[i]) & 0xFF) << " ";
}
cout << "\nДвоичное: ";
for (size_t i = 0; i < sizeof(alice.name); i++)
{
for (int bit = 7; bit >= 0; --bit)
{
cout << ((alice.name[i] & (1 << bit)) ? '1' : '0');
}
cout << " ";
}
cout << "\n\n";
cout << "Год поступления:" << endl;
cout << "Адрес: " << &alice.admissionYear << endl;
cout << "Смещение: " << offsetof(Student, admissionYear) << " байт" << endl;
cout << "Размер: " << sizeof(alice.admissionYear) << " байт" << endl;
cout << "Шестнадцатеричное: " << hex << alice.admissionYear << endl;
cout << "Двоичное: ";
for (int bit = 15; bit >= 0; --bit)
{
cout << ((alice.admissionYear & (1 << bit)) ? '1' : '0');
}
cout << "\n\n";
cout << "Средний балл:" << endl;
cout << "Адрес: " << &alice.averageGrade << endl;
cout << "Смещение: " << offsetof(Student, averageGrade) << " байт" << endl;
cout << "Размер: " << sizeof(alice.averageGrade) << " байт" << endl;
cout << "Шестнадцатеричное: " << hex << *reinterpret_cast<uint32_t*>(&alice.averageGrade) << endl;
cout << "Двоичное: ";
uint32_t averageGradeBits = *reinterpret_cast<uint32_t*>(&alice.averageGrade);
for (int bit = 31; bit >= 0; --bit)
{
cout << ((averageGradeBits & (1 << bit)) ? '1' : '0');
}
cout << "\n\n";
cout << "Пол:" << endl;
cout << "Значение: " << static_cast<int>(alice.gender) << endl;
cout << "Размер (один бит): 1 бит" << endl;
cout << "\n\n";
cout << "Количество пройденных курсов:" << endl;
cout << "Адрес: " << &alice.coursesCount << endl;
cout << "Смещение: " << offsetof(Student, coursesCount) << " байт" << endl;
cout << "Размер: " << sizeof(alice.coursesCount) << " байт" << endl;
cout << "Шестнадцатеричное: " << hex << alice.coursesCount << endl;
cout << "Двоичное: ";
for (int bit = 31; bit >= 0; --bit)
{
cout << ((alice.coursesCount & (1 << bit)) ? '1' : '0');
}
cout << "\n\n";
cout << "4) Все элементы массива в шестнадцатеричном виде:" << endl;
for (int i = 0; i < 3; i++)
{
cout << "Student[" << i << "] = ";
for (size_t j = 0; j < sizeof(students[i]); j++)
{
cout << hex << setw(2) << setfill('0') << (static_cast<int>(reinterpret_cast<uint8_t*>(&students[i])[j]) & 0xFF) << " ";
}
cout << endl;
}
return 0;
}