Created
June 10, 2016 23:27
-
-
Save reedacartwright/a560f4c6ac60f564f89da3f4a79723ed to your computer and use it in GitHub Desktop.
Pack 4 ASCII Nucleotides into 1 byte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#include <cstdint> | |
#include <cstdio> | |
uint8_t pack_a(const char *cs) { | |
uint32_t u; | |
memcpy(&u,cs,4); | |
u &= 0x06060606; | |
u = (u | (u << 6) | (u << 12) | (u << 18)) >> 19; | |
return u; | |
} | |
uint8_t pack_b(const char *cs) { | |
uint32_t u; | |
memcpy(&u,cs,4); | |
u &= 0x06060606; | |
u *= 0x041041; | |
u /= 0x80000; | |
return u; | |
} | |
uint8_t pack_c(const char *cs) { | |
uint32_t u; | |
memcpy(&u,cs,4); | |
return ((u & 0x06060606) * 0x041041)/0x80000; | |
} | |
void unpack(int x, char *cs) { | |
const char ss[] = "ACTG"; | |
for(int i=0;i<4;++i) { | |
cs[i] = ss[x & 0x3]; | |
x = x >> 2; | |
} | |
} | |
int main(int argc, char *argv[] ) { | |
char buffer[5] = "NNNN"; | |
int failures=0; | |
for(int b=0;b<256;++b) { | |
unpack(b,buffer); | |
uint32_t x = pack_c(buffer); | |
if(x != b) | |
failures += 1; | |
printf("%s\t0x%x\t0x%x\t%s\n",buffer,b,x,((x==b) ? "Pass" : "Fail")); | |
} | |
printf("Total failures: %d\n", failures); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment