#include <string>
#include <stdint.h>
#include <cassert>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <bitset>
using namespace std;
typedef uint8_t byte;
const int size = 8;
byte invertEvenSignificantBits(byte x)
{
for (byte mask = 1; mask <= x && mask != 0; mask <<= 2)
x ^= mask;
return x;
}
byte rotateLeft(byte x, byte n)
{
return (x << n) | (x >> (8-n));
}
int main()
{
setlocale(LC_ALL, "Russian");
while (true)
{
int a=0;
int mas[size];
cout<<"\nВведите число по битам: "<<endl;
int result=0;
for (int i = 0; i < size; i++)
{
cin>>mas[i];
if (mas[i]==1)
{
result++;
}
}
bool flag=false;
if (result==1)
{
flag=true;
}
cout << "Принятый код: ";
for (int i = 0; i < size; i++)
{
cout<<mas[i];
}
if (flag==true) // если флаг поднят
{
cout<<endl;
cout<<"\nПреобразованный код: "<<endl;
ostringstream ost;
string s_num;
for (int i = 0; i <8; i++)
{
ost << mas[i];
s_num = ost.str();
}
unsigned long x = bitset<8>(s_num).to_ulong();
bool isPowerOfTwo = (x != 0) && ( (x&(x-1)) == 0 );
x = isPowerOfTwo ? invertEvenSignificantBits(x) : rotateLeft(x, 2);
cout << bitset<8>(x).to_string() << "\n";
}
else{
//иначе
cout<<"\nПреобразованный код: ";
ostringstream ost2;
string s_num2;
for (int i = 0; i <8; i++)
{
ost2 << mas[i];
s_num2 = ost2.str();
}
cout<<bitset<8>(bitset<8>(s_num2).to_ulong()<<2).to_string();
//двигаем биты
}
}
system("pause");
}