Created
November 6, 2018 15:02
-
-
Save vchengsong/255f2251c98d38db45c7d344e1b43262 to your computer and use it in GitHub Desktop.
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 <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; | |
} | |
std::tuple<uint64_t ,uint64_t > index_decode(uint64_t index){ | |
uint64_t symbol = 0, day = 0; | |
day = index & 0xFFFF; | |
index >>= 16; | |
symbol = 0; | |
for ( int i = 0; index & 0x1F && i < 7; i++ ) { | |
symbol |= (index & 0x1F) << (8 * i); | |
index >>= 5; | |
} | |
return { symbol, day }; | |
} | |
int main() { | |
symbol_code = 0x0101010101; | |
uint64_t index = index_encode( symbol_code, 255 ); | |
uint64_t sym1 = 0; | |
uint64_t day1 = 0; | |
std::tie( sym1, day1) = index_decode(index); | |
if (sym1 == symbol_code && day1 == 255) | |
std::cout << "Good" << std::endl; | |
else | |
std::cout << "Bad!" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment