Those. Is it possible to convert into 0000 0011 by means of C ++ from 1100 0000? What do you need to use methods for this?
5 answers
As you know, a byte is an eight-bit binary number. Therefore, it can be represented as such a polynomial:
b1 * 2^7 + b2 * 2^6 + ... + b7 * 2^1 + b8 * 2^0 so or
128 * b1 + 64 * b2 + ... + 2 * b7 + 1 * b8 where b1 - b8 are bits per byte. Therefore, you can get a byte "back to front" like this:
unsigned char Invert(unsigned char x) { int base = 256; unsigned char res = 0; while (x != 0) { res += (x & 1) * (base >>= 1); x >>= 1; } return res; } Here is the language tool (loops and bit operations)
int main() { int a,b; a = 0xC0; for(int i = 0; i < 8; i++) { b = b<<1; b += a%2; a = a>>1; } return 0; } - fourOr a little more in the spirit of such things in Si b = 0 for (i = 0; i <8; i ++) {b << = 1; b | = (a & 1); a >> = 1; } a = b; - avp
template<typename T> const T reversebits(const T& in) { T out = T(0); for (size_t i = 0; i < sizeof(T) * 8; ++i) { out <<= 1; out |= (in >> i)&1; } return out; } Bit-reversible permutation is used in the FFT (Fast Discrete Fourier Transform) algorithm for 2^n points.
The simplest method of implementation is a tabular (256 bytes).
But you can apply the duplication strategy, in which the first tetrads are rearranged in pairs in the first step, in the second - the dyad and in the third - the bits:
abcdefgh - efghabcd - ghefcdab - hgfedcba.
Such an implementation is possible (in place), allowing the use of macros:
b = ((b >> 4) & 15) | ((b & 15) << 4);
b = ((b >> 2) & 51) | ((b & 51) << 2);
b = ((b >> 1) & 85) | ((b & 85) << 1);
This means that 15 = 0b00001111, 51 = 0b00110011, 85 = 0b01010101.
#define INVERT_BYTE(a) ((a&1)<<7) | ((a&2)<<5) | ((a&4)<<3) | ((a&8)<<1) | ((a&16)>>1) | ((a&32)>>3) | ((a&64)>>5) | ((a&128)>>7)
bswapdoes another thing - it swaps bytes - skegg