Advertisement
NolikTop

Untitled

Sep 21st, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <colors.h>
  3.  
  4. using namespace std;
  5.  
  6. void showBits(char b, int order, bool colorAsFloat = false){
  7.     for(int i = 7; i >= 0; --i){
  8.         int currentBit = (b >> i) & 0b1;
  9.  
  10.         if(colorAsFloat){
  11.             int bitOrder = order*8 + 7-i;
  12.  
  13.             switch(bitOrder){
  14.                 case 0: // знак
  15.                     cout << BOLDBLUE;
  16.                     break;
  17.                 case 1: // порядок
  18.                     cout << BOLDGREEN;
  19.                     break;
  20.                 case 9: // мантисса
  21.                     cout << BOLDRED;
  22.                     break;
  23.             }
  24.         }
  25.  
  26.         cout << currentBit;
  27.     }
  28.     cout << " ";
  29. }
  30.  
  31. // аллоцирует новый массив
  32. char* reverseArray(char* a, int size){
  33.     char* b = (char*)malloc(sizeof(char) * size);
  34.  
  35.     for(int i = size - 1, j = 0; i >= 0; --i, ++j){
  36.         b[j] = a[i];
  37.     }
  38.  
  39.     return b;
  40. }
  41.  
  42. void showBytes(char* a, int size, bool colorAsFloat = false){
  43.     for (int i = 0; i < size; ++i) {
  44.         char currentByte = a[i];
  45.         showBits(currentByte, i, colorAsFloat);
  46.     }
  47.     cout << RESET;
  48. }
  49.  
  50. bool isLittleEndian(){
  51.     short tmp = 0x1;
  52.     char* tmpBytes = (char*)&tmp;
  53.  
  54.     return tmpBytes[0] == 0;
  55. }
  56.  
  57. void littleEndianShowBytes(char* a, int size, bool colorAsFloat = false){
  58.     if(!isLittleEndian()){
  59.         a = reverseArray(a, size);
  60.     }
  61.     showBytes(a, size, colorAsFloat);
  62. }
  63.  
  64. void prog1(){
  65.     int a;
  66.     float b;
  67.  
  68.     cout << "Введите целочисленное число: ";
  69.     cin >> a;
  70.     cout << endl << "Введите дробное число: ";
  71.     cin >> b;
  72.  
  73.     char* aBytes = (char*)&a;
  74.     char* bBytes = (char*)&b;
  75.  
  76.     cout << "целочисленное число: ";
  77.     cout << endl << "+ в памяти: ";
  78.     showBytes(aBytes, sizeof(int));
  79.     cout << endl << "+ Little Endian: ";
  80.     littleEndianShowBytes(aBytes, sizeof(a));
  81.  
  82.     cout << endl << "дробное число: ";
  83.     cout << endl << "+ в памяти: ";
  84.     showBytes(bBytes, sizeof(float));
  85.     cout << endl << "+ Little Endian: ";
  86.     littleEndianShowBytes(bBytes, sizeof(b), true);
  87. }
  88.  
  89. void prog2(){
  90.     float a;
  91.  
  92.     cout << "Введите дробное число: ";
  93.     cin >> a;
  94.  
  95.     char* aBytes = (char*)&a;
  96.  
  97.     cout << endl << "структура до изменения: ";
  98.     littleEndianShowBytes((char*)&a, sizeof(a), true);
  99.     cout << endl << endl;
  100.  
  101.     const int maxBitOrder = sizeof(a)*8 - 1;
  102.  
  103.     int changingBitOrder;
  104.     cout << "Введите номер изменяемого бита (от меньшего разряда, с 0): ";
  105.     cin >> changingBitOrder;
  106.     if(changingBitOrder < 0 || changingBitOrder > maxBitOrder){
  107.         cout << "Некорректное значение порядка (с 0 до " << maxBitOrder << " должно быть)";
  108.         return;
  109.     }
  110.  
  111.     int bitValue;
  112.     cout << endl << "Укажите значение бита (0/1): ";
  113.     cin >> bitValue;
  114.  
  115.     if(bitValue != 0 && bitValue != 1){
  116.         cout << "Некорректное значение бита";
  117.         return;
  118.     }
  119.  
  120.     int mask = 1 << changingBitOrder;
  121.  
  122.     int aInt = *((int*)&a);
  123.     int currentBitValue = (aInt & mask) >> changingBitOrder;
  124.     if(currentBitValue != changingBitOrder){
  125.         aInt ^= mask;
  126.     }
  127.  
  128.     a = *((float*)&aInt);
  129.  
  130.     cout << endl << endl;
  131.  
  132.     cout << "структура после изменения: ";
  133.     littleEndianShowBytes(aBytes, sizeof(a), true);
  134.  
  135.     cout << endl << "новое дробное число: " << a;
  136. }
  137.  
  138. int main(){
  139.     prog1();
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement