Skip to content

Instantly share code, notes, and snippets.

@tnn2
Created March 1, 2019 08:55
Show Gist options
  • Save tnn2/5febc4208e3797dcda2e106b385bf5aa to your computer and use it in GitHub Desktop.
Save tnn2/5febc4208e3797dcda2e106b385bf5aa to your computer and use it in GitHub Desktop.
transpose.c
#include <stdio.h>
/*
* transpose8x8(p, q):
*
* Transpose an 8x8 matrix of boolean values.
* The transpose operation switches rows into columns and vice versa.
* The booleans are represented with 1 bit each and packed into 8 bytes
* for a total of 64 bits.
*
* For example, the input array:
* p = {0x04, 0xFF, 0x84, 0x84, 0x04, 0x04, 0x04, 0x04}
* results in q = {0x02, 0x02, 0xFF, 0x02, 0x02, 0x02, 0x02, 0x0E}
*
* Because:
*
* p[n] | 01234567 q[n] | 01234567
* -----+------------------------+-----------------
* 0x04 | --a----- 0x02 | -b------
* 0xFF | bcdefghi 0x02 | -c------
* 0x84 | --j----k 0xFF | adjlnopq
* 0x84 | --l----m ===> 0x02 | -e------
* 0x04 | --n----- ===> 0x02 | -f------
* 0x04 | --o----- 0x02 | -g------
* 0x04 | --p----- 0x02 | -h------
* 0x04 | --q----- 0x0E | -ikm----
*/
void
transpose8x8(unsigned char p[8], unsigned char q[8])
{
/* your code here */
}
int
main(int argc, char **argv)
{
unsigned char p[8] = { 0x04, 0xFF, 0x84, 0x84, 0x04, 0x04, 0x04, 0x04 };
unsigned char q[8] = { 0 };
transpose8x8(p, q);
/* draw some ascii art to stdout */
for (int m = 0; m < 8; m++) {
printf("%02X ", p[m]);
for (int n = 0; n < 8; n++) {
printf("%c", (p[m] >> n) & 1 ? '#' : '-');
}
printf(" %02X ", q[m]);
for (int n = 0; n < 8; n++) {
printf("%c", (q[m] >> n) & 1 ? '#' : '-');
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment