转换/旋转字节数组

时间:2018-01-19 17:53:22

标签: c++ c arrays transform bit

我正在寻找一种方法来快速旋转字节数组中的位,而不需要大量的for循环来获取并将每个位从旧位置设置为新位置。在这种特殊情况下,我需要顺时针旋转90度。 所以,例如,如果我有:

unsigned char array[8] = {
0b11110000,
0b10000000,
0b10000000,
0b10000000,
0b00000000,
0b00000000,
0b00000000,
0b00000001
};

我需要得到:

unsigned char array[8] = {
0b00001111,
0b00000001,
0b00000001,
0b00000001,
0b00000000,
0b00000000,
0b00000000,
0b10000000
};

寻找最有效的方法来做到这一点......但它可能并不总是8x8的网格。永远是2维的力量(如果这有帮助!)

1 个答案:

答案 0 :(得分:1)

繁琐但没有for循环:

rotatedArray[0] = (array[7] & 128) || (array[6] & 128) >> 1 || (array[5] & 128) >> 2 || (array[4] & 128) >> 3 || (array[3] & 128) >> 4 || (array[2] & 128) >> 5 || (array[1] & 128) >> 6 || (array[0] & 128) >> 7;

rotatedArray[1] = (array[7] & 64) << 1 || (array[6] & 64) || (array[5] & 64) >> 1 || (array[4] & 64) >> 2 || (array[3] & 64) >> 3 || (array[2] & 64) >> 4 || (array[1] & 64) >> 5 || (array[0] & 64) >> 6;

rotatedArray[2] = (array[7] & 32) << 2 || (array[6] & 32) << 1|| (array[5] & 32) || (array[4] & 32) >> 1 || (array[3] & 32) >> 2 || (array[2] & 32) >> 3 || (array[1] & 32) >> 4 || (array[0] & 32) >> 5;

...

rotatedArray[7] = (array[7] & 1) << 7 || (array[6] & 1) << 6 || (array[5] & 1) << 5 || (array[4] & 1) << 4 || (array[3] & 1) << 3 || (array[2] & 1) << 2 || (array[1] & 1) << 1 || (array[0] & 1);

修改

如果画布限制为32x8(8个32位整数),结果应为4个8x8块,则不使用for循环:

rotatedArray[0][0] = (array[7] & 0x80000000) >> 24 || (array[6] & 0x80000000) >> 25 || (array[5] & 0x80000000) >> 26 || (array[4] & 0x80000000) >> 27 || (array[3] & 0x80000000) >> 28 || (array[2] & 0x80000000) >> 29 || (array[1] & 0x80000000) >> 30 || (array[0] & 0x80000000) >> 31;
rotatedArray[0][1] = (array[7] & 0x40000000) >> 23 || (array[6] & 0x40000000) >> 24 || (array[5] & 0x40000000) >> 25 || (array[4] & 0x40000000) >> 26 || (array[3] & 0x40000000) >> 27 || (array[2] & 0x40000000) >> 28 || (array[1] & 0x40000000) >> 29 || (array[0] & 0x40000000) >> 30;
rotatedArray[0][2] = (array[7] & 0x20000000) >> 22 || (array[6] & 0x2000000) >> 23 || (array[5] & 0x20000000) >> 24 || (array[4] & 0x20000000) >> 25 || (array[3] & 0x20000000) >> 26 || (array[2] & 0x20000000) >> 27 || (array[1] & 0x20000000) >> 28 || (array[0] & 0x20000000) >> 29;
rotatedArray[0][3] = (array[7] & 0x10000000) >> 21 || (array[6] & 0x10000000) >> 22 || (array[5] & 0x10000000) >> 23 || (array[4] & 0x10000000) >> 24 || (array[3] & 0x10000000) >> 25 || (array[2] & 0x10000000) >> 26 || (array[1] & 0x10000000) >> 27 || (array[0] & 0x10000000) >> 28;
rotatedArray[0][4] = (array[7] & 0x8000000) >> 20 || (array[6] & 0x8000000) >> 21 || (array[5] & 0x8000000) >> 22 || (array[4] & 0x8000000) >> 23 || (array[3] & 0x8000000) >> 24 || (array[2] & 0x8000000) >> 25 || (array[1] & 0x8000000) >> 26 || (array[0] & 0x8000000) >> 27;
rotatedArray[0][5] = (array[7] & 0x4000000) >> 19 || (array[6] & 0x4000000) >> 20 || (array[5] & 0x4000000) >> 21 || (array[4] & 0x4000000) >> 22 || (array[3] & 0x4000000) >> 23 || (array[2] & 0x4000000) >> 24 || (array[1] & 0x4000000) >> 25 || (array[0] & 0x4000000) >> 26;
rotatedArray[0][6] = (array[7] & 0x2000000) >> 18 || (array[6] & 0x2000000) >> 19 || (array[5] & 0x2000000) >> 20 || (array[4] & 0x2000000) >> 21 || (array[3] & 0x2000000) >> 22 || (array[2] & 0x2000000) >> 23 || (array[1] & 0x2000000) >> 24 || (array[0] & 0x2000000) >> 25;
rotatedArray[0][7] = (array[7] & 0x1000000) >> 17 || (array[6] & 0x1000000) >> 18 || (array[5] & 0x1000000) >> 19 || (array[4] & 0x1000000) >> 20 || (array[3] & 0x1000000) >> 21 || (array[2] & 0x1000000) >> 22 || (array[1] & 0x1000000) >> 23 || (array[0] & 0x1000000) >> 24;

rotatedArray[1][0] = (array[7] & 0x800000) >> 16 || (array[6] & 0x800000) >> 17 || (array[5] & 0x800000) >> 18 || (array[4] & 0x800000) >> 19 || (array[3] & 0x800000) >> 20 || (array[2] & 0x800000) >> 21 || (array[1] & 0x800000) >> 22 || (array[0] & 0x800000) >> 23;
rotatedArray[1][1] = (array[7] & 0x400000) >> 15 || (array[6] & 0x400000) >> 16 || (array[5] & 0x400000) >> 17 || (array[4] & 0x400000) >> 18 || (array[3] & 0x400000) >> 19 || (array[2] & 0x400000) >> 20 || (array[1] & 0x400000) >> 21 || (array[0] & 0x400000) >> 22;
rotatedArray[1][2] = (array[7] & 0x200000) >> 14 || (array[6] & 0x200000) >> 15 || (array[5] & 0x200000) >> 16 || (array[4] & 0x200000) >> 17 || (array[3] & 0x200000) >> 18 || (array[2] & 0x200000) >> 19 || (array[1] & 0x200000) >> 20 || (array[0] & 0x200000) >> 21;
rotatedArray[1][3] = (array[7] & 0x100000) >> 13 || (array[6] & 0x100000) >> 14 || (array[5] & 0x100000) >> 15 || (array[4] & 0x100000) >> 16 || (array[3] & 0x100000) >> 17 || (array[2] & 0x100000) >> 18 || (array[1] & 0x100000) >> 19 || (array[0] & 0x100000) >> 20;
rotatedArray[1][4] = (array[7] & 0x80000) >> 12 || (array[6] & 0x80000) >> 13 || (array[5] & 0x80000) >> 14 || (array[4] & 0x80000) >> 15 || (array[3] & 0x80000) >> 16 || (array[2] & 0x80000) >> 17 || (array[1] & 0x80000) >> 18 || (array[0] & 0x80000) >> 19;
rotatedArray[1][5] = (array[7] & 0x40000) >> 11 || (array[6] & 0x40000) >> 12 || (array[5] & 0x40000) >> 13 || (array[4] & 0x40000) >> 14 || (array[3] & 0x40000) >> 15 || (array[2] & 0x40000) >> 16 || (array[1] & 0x40000) >> 17 || (array[0] & 0x40000) >> 18;
rotatedArray[1][6] = (array[7] & 0x20000) >> 10 || (array[6] & 0x20000) >> 11 || (array[5] & 0x20000) >> 12 || (array[4] & 0x20000) >> 13 || (array[3] & 0x20000) >> 14 || (array[2] & 0x20000) >> 15 || (array[1] & 0x20000) >> 16 || (array[0] & 0x20000) >> 17;
rotatedArray[1][7] = (array[7] & 0x10000) >> 9 || (array[6] & 0x10000) >> 10 || (array[5] & 0x10000) >> 11 || (array[4] & 0x10000) >> 12 || (array[3] & 0x10000) >> 13 || (array[2] & 0x10000) >> 14 || (array[1] & 0x10000) >> 15 || (array[0] & 0x10000) >> 16;

rotatedArray[2][0] = (array[7] & 0x8000) >> 8 || (array[6] & 0x8000) >> 9 || (array[5] & 0x8000) >> 10 || (array[4] & 0x8000) >> 11 || (array[3] & 0x8000) >> 12 || (array[2] & 0x8000) >> 13 || (array[1] & 0x8000) >> 14 || (array[0] & 0x8000) >> 15;
rotatedArray[2][1] = (array[7] & 0x4000) >> 7 || (array[6] & 0x4000) >> 8 || (array[5] & 0x4000) >> 9 || (array[4] & 0x4000) >> 10 || (array[3] & 0x4000) >> 11 || (array[2] & 0x4000) >> 12 || (array[1] & 0x4000) >> 13 || (array[0] & 0x4000) >> 14;
rotatedArray[2][2] = (array[7] & 0x2000) >> 6 || (array[6] & 0x2000) >> 7 || (array[5] & 0x2000) >> 8 || (array[4] & 0x2000) >> 9 || (array[3] & 0x2000) >> 10 || (array[2] & 0x2000) >> 11 || (array[1] & 0x2000) >> 12 || (array[0] & 0x2000) >> 13;
rotatedArray[2][3] = (array[7] & 0x1000) >> 5 || (array[6] & 0x1000) >> 6 || (array[5] & 0x1000) >> 7 || (array[4] & 0x1000) >> 8 || (array[3] & 0x1000) >> 9 || (array[2] & 0x1000) >> 10 || (array[1] & 0x1000) >> 11 || (array[0] & 0x1000) >> 12;
rotatedArray[2][4] = (array[7] & 0x800) >> 4 || (array[6] & 0x800) >> 5 || (array[5] & 0x800) >> 6 || (array[4] & 0x800) >> 7 || (array[3] & 0x800) >> 8 || (array[2] & 0x800) >> 9 || (array[1] & 0x800) >> 10 || (array[0] & 0x800) >> 11;
rotatedArray[2][5] = (array[7] & 0x400) >> 3 || (array[6] & 0x400) >> 4 || (array[5] & 0x400) >> 5 || (array[4] & 0x400) >> 6 || (array[3] & 0x400) >> 7 || (array[2] & 0x400) >> 8 || (array[1] & 0x400) >> 9 || (array[0] & 0x400) >> 10;
rotatedArray[2][6] = (array[7] & 0x200) >> 2 || (array[6] & 0x200) >> 3 || (array[5] & 0x200) >> 4 || (array[4] & 0x200) >> 5 || (array[3] & 0x200) >> 6 || (array[2] & 0x200) >> 7 || (array[1] & 0x200) >> 8 || (array[0] & 0x200) >> 9;
rotatedArray[2][7] = (array[7] & 0x100) >> 1 || (array[6] & 0x100) >> 2 || (array[5] & 0x100) >> 3 || (array[4] & 0x100) >> 4 || (array[3] & 0x100) >> 5 || (array[2] & 0x100) >> 6 || (array[1] & 0x100) >> 7 || (array[0] & 0x100) >> 8;

rotatedArray[3][0] = (array[7] & 0x80) || (array[6] & 0x80) >> 1 || (array[5] & 0x80) >> 2 || (array[4] & 0x80) >> 3 || (array[3] & 0x80) >> 4 || (array[2] & 0x80) >> 5 || (array[1] & 0x80) >> 6 || (array[0] & 0x80) >> 7;
rotatedArray[3][1] = (array[7] & 0x40) << 1 || (array[6] & 0x40) || (array[5] & 0x40) >> 1 || (array[4] & 0x40) >> 2 || (array[3] & 0x40) >> 3 || (array[2] & 0x40) >> 4 || (array[1] & 0x40) >> 5 || (array[0] & 0x40) >> 6;
rotatedArray[3][2] = (array[7] & 0x20) << 2 || (array[6] & 0x20) << 1 || (array[5] & 0x20) || (array[4] & 0x20) >> 1 || (array[3] & 0x20) >> 2 || (array[2] & 0x20) >> 3 || (array[1] & 0x20) >> 4 || (array[0] & 0x20) >> 5;
rotatedArray[3][3] = (array[7] & 0x10) << 3 || (array[6] & 0x10) << 2 || (array[5] & 0x10) << 1 || (array[4] & 0x10) || (array[3] & 0x10) >> 1 || (array[2] & 0x10) >> 2 || (array[1] & 0x10) >> 3 || (array[0] & 0x10) >> 4;
rotatedArray[3][4] = (array[7] & 0x8) << 4 || (array[6] & 0x8) << 3 || (array[5] & 0x8) << 2 || (array[4] & 0x8) << 1 || (array[3] & 0x8) || (array[2] & 0x8) >> 1 || (array[1] & 0x8) >> 2 || (array[0] & 0x8) >> 3;
rotatedArray[3][5] = (array[7] & 0x4) << 5 || (array[6] & 0x4) << 4 || (array[5] & 0x4) << 3 || (array[4] & 0x4) << 2 || (array[3] & 0x4) << 1 || (array[2] & 0x4) || (array[1] & 0x4) >> 1 || (array[0] & 0x4) >> 2;
rotatedArray[3][6] = (array[7] & 0x2) << 6 || (array[6] & 0x2) << 5 || (array[5] & 0x2) << 4 || (array[4] & 0x2) << 3 || (array[3] & 0x2) << 2 || (array[2] & 0x2) << 1 || (array[1] & 0x2) || (array[0] & 0x2) >> 1;
rotatedArray[3][7] = (array[7] & 0x1) << 7 || (array[6] & 0x1) << 6 || (array[5] & 0x1) << 5 || (array[4] & 0x1) << 4 || (array[3] & 0x1) << 3 || (array[2] & 0x1) << 2 || (array[1] & 0x1) << 1 || (array[0] & 0x1);

免责声明可能会出现一些错误,但您明白这一点。