|
|
@ -1,88 +1,72 @@
|
|
|
|
#include <iostream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cassert>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <bitset>
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char nibble_to_hex(uint8_t i) {
|
|
|
|
|
|
|
|
char digits[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
assert(0x0 <= i && i <= 0xf);
|
|
|
|
|
|
|
|
return digits[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void print_in_hex(uint8_t byte) {
|
|
|
|
|
|
|
|
std::cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0xf);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ôóíêöèÿ äëÿ ïðåîáðàçîâàíèÿ òèïîâ
|
|
|
|
struct Student {
|
|
|
|
const uint8_t* as_bytes(const void* data) {
|
|
|
|
char name[17];
|
|
|
|
return reinterpret_cast<const uint8_t*>(data);
|
|
|
|
uint16_t admissionYear;
|
|
|
|
}
|
|
|
|
float averageGrade;
|
|
|
|
|
|
|
|
bool isMale : 1;
|
|
|
|
// Ôóíêöèÿ äëÿ ïå÷àòè ìàññèâà áàéòîâ
|
|
|
|
uint8_t completedCourses;
|
|
|
|
void print_in_hex(const void* data, size_t size) {
|
|
|
|
Student* groupLeader;
|
|
|
|
const uint8_t* bytes = as_bytes(data);
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
|
|
|
|
print_in_hex(bytes[i]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Äëÿ óäîáñòâà ÷òåíèÿ: ïðîáåëû ìåæäó áàéòàìè, ïî 16 áàéò íà ñòðîêó.
|
|
|
|
int main() {
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
const int numberOfStudents = 3;
|
|
|
|
std::cout << '\n';
|
|
|
|
Student students[numberOfStudents];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
std::cout << ' ';
|
|
|
|
students[0].admissionYear = 2023;
|
|
|
|
}
|
|
|
|
students[0].averageGrade = 4.5;
|
|
|
|
}
|
|
|
|
students[0].isMale = true;
|
|
|
|
std::cout << std::endl;
|
|
|
|
students[0].completedCourses = 5;
|
|
|
|
}
|
|
|
|
students[0].groupLeader = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
// Ôóíêöèÿ äëÿ ïðîâåðêè áèòîâ è âûâîäà èõ â äâîè÷íîé ñèñòåìå
|
|
|
|
students[1].admissionYear = 2022;
|
|
|
|
char bit_digit(uint8_t byte, uint8_t bit) {
|
|
|
|
students[1].averageGrade = 3.8;
|
|
|
|
if (byte & (0x1 << bit)) {
|
|
|
|
students[1].isMale = false;
|
|
|
|
return '1';
|
|
|
|
students[1].completedCourses = 6;
|
|
|
|
|
|
|
|
students[1].groupLeader = &students[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
students[2].admissionYear = 2021;
|
|
|
|
|
|
|
|
students[2].averageGrade = 3.2;
|
|
|
|
|
|
|
|
students[2].isMale = true;
|
|
|
|
|
|
|
|
students[2].completedCourses = 7;
|
|
|
|
|
|
|
|
students[2].groupLeader = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numberOfStudents; ++i) {
|
|
|
|
|
|
|
|
cout << "\nÈíôîðìàöèÿ î ñòóäåíòå " << i + 1 << ":\n";
|
|
|
|
|
|
|
|
cout << "Àäðåñ ïîëÿ admissionYear: " << static_cast<void*>(&students[i].admissionYear) << ", Ñìåùåíèå: "
|
|
|
|
|
|
|
|
<< offsetof(Student, admissionYear) << ", Ðàçìåð: " << sizeof(students[i].admissionYear) << " áàéò, "
|
|
|
|
|
|
|
|
<< "Øåñòíàäöàòåðè÷íîå: " << hex << students[i].admissionYear << ", Äâîè÷íîå: "
|
|
|
|
|
|
|
|
<< bitset<16>(students[i].admissionYear) << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Àäðåñ ïîëÿ averageGrade: " << static_cast<void*>(&students[i].averageGrade) << ", Ñìåùåíèå: "
|
|
|
|
|
|
|
|
<< offsetof(Student, averageGrade) << ", Ðàçìåð: " << sizeof(students[i].averageGrade) << " áàéò, "
|
|
|
|
|
|
|
|
<< "Çíà÷åíèå: " << students[i].averageGrade << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Àäðåñ è áèòîâîå ïîëå isMale: " << static_cast<void*>(&students[i].isMale) << ", Ñìåùåíèå: "
|
|
|
|
|
|
|
|
<< offsetof(Student, isMale) << ", Ðàçìåð: " << sizeof(students[i].isMale) << " áèò, "
|
|
|
|
|
|
|
|
<< "Çíà÷åíèå: " << (students[i].isMale ? "ìóæñêîé" : "æåíñêèé") << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Àäðåñ ïîëÿ completedCourses: " << static_cast<void*>(&students[i].completedCourses) << ", Ñìåùåíèå: "
|
|
|
|
|
|
|
|
<< offsetof(Student, completedCourses) << ", Ðàçìåð: " << sizeof(students[i].completedCourses) << " áàéò, "
|
|
|
|
|
|
|
|
<< "Çíà÷åíèå: " << static_cast<int>(students[i].completedCourses) << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << "Àäðåñ ïîëÿ groupLeader: " << static_cast<void*>(&students[i].groupLeader) << ", Ñìåùåíèå: "
|
|
|
|
|
|
|
|
<< offsetof(Student, groupLeader) << ", Ðàçìåð: " << sizeof(students[i].groupLeader) << " áàéò, "
|
|
|
|
|
|
|
|
<< "Øåñòíàäöàòåðè÷íîå: " << hex << reinterpret_cast<uintptr_t>(students[i].groupLeader) << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Âûâîäèì ïðåäñòàâëåíèå ñòðóêòóðû â ïàìÿòè
|
|
|
|
|
|
|
|
cout << "Ïðåäñòàâëåíèå â ïàìÿòè (â øåñòíàäöàòåðè÷íîé ñèñòåìå):\n";
|
|
|
|
|
|
|
|
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&students[i]);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < sizeof(Student); ++j) {
|
|
|
|
|
|
|
|
cout << hex << setfill('0') << setw(2) << static_cast<int>(bytes[j]) << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '0';
|
|
|
|
cout << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ôóíêöèÿ äëÿ ïå÷àòè áàéòà â äâîè÷íîé ñèñòåìå
|
|
|
|
|
|
|
|
void print_in_binary(uint8_t byte) {
|
|
|
|
|
|
|
|
for (uint8_t bit = 7; bit > 0; bit--) {
|
|
|
|
|
|
|
|
std::cout << bit_digit(byte, bit);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
uint8_t u8 = 0x42;
|
|
|
|
|
|
|
|
uint16_t u16 = 0x42;
|
|
|
|
|
|
|
|
uint32_t u32 = 0x42;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "u8 bytes: ";
|
|
|
|
|
|
|
|
print_in_hex(&u8, sizeof(u8));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "\nu8 binary: ";
|
|
|
|
|
|
|
|
print_in_binary(u8);
|
|
|
|
|
|
|
|
std::cout << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "\nu16 bytes: ";
|
|
|
|
|
|
|
|
print_in_hex(&u16, sizeof(u16));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "\nu16 binary: ";
|
|
|
|
|
|
|
|
print_in_binary(u16);
|
|
|
|
|
|
|
|
std::cout << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "\nu32 bytes: ";
|
|
|
|
|
|
|
|
print_in_hex(&u32, sizeof(u32));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "\nu32 binary: ";
|
|
|
|
|
|
|
|
print_in_binary(u32);
|
|
|
|
|
|
|
|
std::cout << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|