Skip to content

Instantly share code, notes, and snippets.

@vchengsong
Created November 6, 2018 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vchengsong/50c4dd1bcfc52b0d26b9c1013694bcee to your computer and use it in GitHub Desktop.
Save vchengsong/50c4dd1bcfc52b0d26b9c1013694bcee to your computer and use it in GitHub Desktop.
#include <iostream>
/*
* 原symbol_code最大允许7个大写字符,可以用5个位表示一个大写字符,5*7=35,也就是用5个字节就可以容纳了。
* 天数为1-365,用2个字节。
*/
uint64_t symbol_code;
uint64_t day;
uint64_t index_encode( uint64_t sym, uint64_t day){
uint64_t res = 0;
for ( int i = 0; sym & 0xFF && i < 7; i++ ) {
res |= (sym & 0x1F) << (5 * i);
sym >>= 8;
}
res <<= 16;
res |= day & 0xFFFF;
return res;
}
void index_decode(uint64_t index,uint64_t& symbol, uint64_t& day){
day = index & 0xFFFF;
index >>= 16;
symbol = 0;
for ( int i = 0; index & 0x1F && i < 7; i++ ) {
symbol |= (index & 0x1F) << (8 * i);
index >>= 5;
}
}
int main() {
symbol_code = 0x0101010101;
uint64_t index = index_encode( symbol_code, 255 );
uint64_t sym1 = 0;
uint64_t day1 = 0;
index_decode(index, sym1, day1 );
std::cout << "Hello, World!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment