#include <iostream>
#include <windows.h>
#include <assert.h>
using namespace std;

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) {
	cout << nibble_to_hex(byte >> 4)
		<< nibble_to_hex(byte & 0xf);
}

const uint8_t* as_bytes(const void* data)
{
	return reinterpret_cast<const uint8_t*>(data);
}

void print_in_hex(const void* data, size_t ������) {
	const uint8_t* bytes = as_bytes(data);	
	for (size_t i = 0; i < ������; i++) {
		print_in_hex(bytes[i]);
		
		if ((i + 1) % 16 == 0) {	
			cout << '\n';
		}
		else {
			cout << ' ';
		}
	}
}

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 ������) {
	const uint8_t* bytes = as_bytes(data);
	for (size_t i = 0; i < ������; i++) {
		print_in_binary(bytes[i]);
		
		if ((i + 1) % 4 == 0) {
			cout << '\n';
		}
		else {
			cout << ' ';
		}
	}
}

struct Student
{
	char name[17];	
	uint16_t year;	
	float avg_mark;	
	uint32_t gender : 1;	//���, �������������� ����� ����� (0 � �������, 1 � �������);
	uint32_t ncourses;	//���������� ���������� ������
	Student* chief;		//��������� �� ��������� Student, ����������� �������� ������
};
#define NSTUDENTS 3


int main()
{
	setlocale(LC_ALL, "Russian");//������� ������
	SetConsoleCP(1251); //��������� ������� �������� win-cp 1251 � ����� �����
	SetConsoleOutputCP(1251); //��������� ������� �������� win-cp 1251 � ����� ������
	char t[32];
	//�������� � ��������� ������ �� ���� �������� Student, ����������� ����
	//��������� ����� ������ � �� ��������
	Student group[NSTUDENTS] = { 0 };
	for (int i = 0; i < NSTUDENTS; i++)
	{
		cout << "��� ��������: ";
		cin.getline(group[i].name, sizeof(group[i].name));
		cout << "��� �����������: ";
		cin >> group[i].year;
		cout << "������� ����: ";
		cin >> group[i].avg_mark;
		cout << "��� (0 - �, 1 - �): ";
		uint32_t tmp;
		cin >> tmp;
		assert(0 <= tmp && tmp <= 1);
		group[i].gender = tmp;
		cout << "���������� ���������� ������: ";
		cin >> tmp;
		group[i].ncourses = tmp;
		cin.getline(t, sizeof(t));		//��������� ������� ������ ����� ����� ���������� �����
	}
	//�������� �������� ��������� �������
	int chief = rand() % NSTUDENTS;
	cout << "�������� ������ " << group[chief].name << endl;
	//����������� ��������� �� ��������
	for (int i = 0; i < NSTUDENTS; i++)
	{
		if (i == chief)
			group[i].chief = NULL;
		else
			group[i].chief = &group[chief];
	}
	//������� ����� � ������ �������
	cout << "����� = " << group << endl;
	cout << "������ ������� = " << sizeof(group) << endl;
	//������ � ������� ���� ��������� �������;
	for (int i = 0; i < NSTUDENTS; i++)
	{
		cout << "�����[" << i << "] = " << &group[i] << endl;
		cout << "������ �������[" << i << "] = " << sizeof(group[i]) << endl;
	}
	int s;
	for (s = 0; s < NSTUDENTS && s == chief; s++);		//�������� �� ��������
														//��� ���� �����, ����� ����
	cout << "������� " << group[s].name << ":" << endl;
	//�����, �������� �� ������ ���������, ������
	cout << "���: ����� = " << &group[s].name << " ������ = " << sizeof(group[s].name) << " �������� = " << offsetof(Student, name) << endl;
	//����������������� �������������
	cout << "hex: " << endl;
	print_in_hex(group[s].name, sizeof(group[s].name));
	cout << endl;
	//�������� �������������
	cout << "bin: " << endl;
	print_in_binary(group[s].name, sizeof(group[s].name));
	cout << endl;
	//�����, �������� �� ������ ���������, ������
	cout << "���: ����� = " << &group[s].year << " ������ = " << sizeof(group[s].year) << " �������� = " << offsetof(Student, year) << endl;
	//����������������� �������������
	cout << "hex: " << endl;
	print_in_hex(&group[s].year, sizeof(group[s].year));
	cout << endl;
	//�������� �������������
	cout << "bin: " << endl;
	print_in_binary(&group[s].year, sizeof(group[s].year));
	cout << endl;
	//�����, �������� �� ������ ���������, ������
	cout << "������� ����: ����� = " << &group[s].avg_mark << " ������ = " << sizeof(group[s].avg_mark) << " �������� = " << offsetof(Student, avg_mark) << endl;
	//����������������� �������������
	cout << "hex: " << endl;
	print_in_hex(&group[s].avg_mark, sizeof(group[s].avg_mark));
	cout << endl;
	//�������� �������������
	cout << "bin: " << endl;
	print_in_binary(&group[s].avg_mark, sizeof(group[s].avg_mark));
	cout << endl;
	//�����, �������� �� ������ ���������, ������
	cout << "ncourses: ����� = " << &group[s].ncourses << " ������ = " << sizeof(group[s].ncourses) << " �������� = " << offsetof(Student, ncourses) << endl;
	//����������������� �������������
	cout << "hex: " << endl;
	print_in_hex(&group[s].ncourses, sizeof(group[s].ncourses));
	cout << endl;
	//�������� �������������
	cout << "bin: " << endl;
	print_in_binary(&group[s].ncourses, sizeof(group[s].ncourses));
	cout << endl;
	//�����, �������� �� ������ ���������, ������
	cout << "��������: ����� = " << &group[s].chief << " ������ = " << sizeof(group[s].chief) << " �������� = " << offsetof(Student, chief) << endl;
	//����������������� �������������
	cout << "hex: " << endl;
	print_in_hex(&group[s].chief, sizeof(group[s].chief));
	cout << endl;
	//�������� �������������
	cout << "bin: " << endl;
	print_in_binary(&group[s].chief, sizeof(group[s].chief));
	cout << endl;
	//��� �������� ������� � ����������������� ����
	cout << "���� ������:" << endl;
	print_in_hex(group, sizeof(group));
	cout << endl;
	system("pause");
	return 0;

}