#include <iostream>
#include <cmath>
using namespace std;
union data_double
{
double data;
char mas[64];
};
void print_mas(char mas[], int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << mas[i];
}
cout << endl;
}
void zero_mas(char mas[], int size)
{
int i;
for (i = 0; i < size; i++)
{
mas[i] = '0';
}
}
void output_unsigned_char(unsigned char value, char mas[])
{
int i, d, j = 0;
for (i = 7; i >= 0; i--) {
d = ((value >> i) & 1);
if (d == 0) {
cout << '0';
mas[j] = '0';
}
else if (d == 1) {
cout << '1';
mas[j] = '1';
}
j++;
}
cout << endl;
}
void output_double(data_double Doub, char mas[])
{
int i, j, k = 0;
for (i = sizeof(double) - 1; i >= 0; i--)
{
unsigned char mask = 128;
for (j = 0; j < sizeof(double); j++)
{
if (Doub.mas[i] & mask)
{
cout << '1';
mas[k] = '1';
k++;
}
else
{
cout << '0';
mas[k] = '0';
k++;
}
mask >>= 1;
}
}
cout << endl;
}
void make_shift(char mas[], int digit, int num, char nmas[], int size) {
for (int i = 0; i < size; i++) {
nmas[i] = mas[i];
}
for (int i = digit - num ; i <= digit-1; i++) {
if (i != size - 1) {
if (mas[i] == '1') {
nmas[i + 1] = '1';
}
}
else break;
}
for (int i = digit - 1; i >= digit - num; i--) {
if (i != 0) {
if (mas[i] == '0') {
nmas[i - 1] = '0';
}
}
else break;
}
}
unsigned char make_unsigned_char(char mas[]) {
unsigned char num = '0';
num >>= 6;
int k = 0;
for (int i = 7; i > 0; i--) {
if (mas[i] == '1') num += pow(2, k);
k++;
}
return num;
}
double make_double(char bits[]){
int i, j = 10, exp = 0;
char sign = 0;
double temp = 0, IsxDouble = 0;
double FloatPart;
sign = bits[0];
for (i = 1; i < 12; i++) {
temp = pow(2, j);
if (bits[i] == '1')
{
exp = exp + temp;
}
j--;
}
exp = exp - 1023;
FloatPart = 0;
FloatPart = pow(2, exp);
j = exp - 1;
for (i = 12; i < 64; i++)
{
temp = pow(2, j);
if (bits[i] == '1')
{
FloatPart = FloatPart + temp;
}
j--;
}
IsxDouble = FloatPart;
if (exp == -1023) {
IsxDouble = 0;
}
if (sign == '1') {
IsxDouble = IsxDouble * -1;
}
return IsxDouble;
}
int main() {
char dmas[64], ucmas[8];
char new_dmas[64], new_ucmas[8];
zero_mas(new_dmas, 64);
zero_mas(new_ucmas, 8);
unsigned char charnum, new_charnum;
double new_doubnum;
int type, hdigit, nbits;
cout << "Please, chose type of input" << endl;
cout << "1: unsigned char" << '\n' << "2: double" << endl;
cin >> type;
if (type == 1) {
cout << "Please write a unsigned char number" << endl;
cin >> charnum;
output_unsigned_char(charnum, ucmas);
while (true) {
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Enter the number of the highest digit and the number of bits to make the shift\n";
cout << "\nHighest digit (from 1 to 8): ";
cin >> hdigit;
cout << "\nNumber of bits (from 1 to 8): ";
cin >> nbits;
cout << endl;
if (hdigit >= nbits) break;
else {
cout << "The number of bits must not exceed the number of the highest digit\n";
}
}
make_shift(ucmas, hdigit, nbits, new_ucmas, 8);
print_mas(new_ucmas, 8);
new_charnum = make_unsigned_char(new_ucmas);
cout << new_charnum;
}
else if (type == 2)
{
data_double doublenum;
cout << "Please, write a double number" << endl;
cin >> doublenum.data;
output_double(doublenum, dmas);
while (true) {
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Enter the number of the highest digit and the number of bits to make the shift\n"
<< "\nHighest digit (from 1 to 64): ";
cin >> hdigit;
cout << "\nNumber of bits (from 1 to 64): ";
cin >> nbits;
cout << endl;
if (hdigit >= nbits) break;
else {
cout << "The number of bits must not exceed the number of the highest digit\n";
}
}
make_shift(dmas, hdigit, nbits, new_dmas, 64);
print_mas(new_dmas, 64);
new_doubnum = make_double(new_dmas);
cout << new_doubnum;
}
return 0;
}