Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First Byte following can encode values
- bytes between
- xxxxxxx1 - [ 0; 2^ 7-1 ] // xxxx contains low bits of value
- xxxxxx10 [1 byte] [ 0; 2^14-1 ]
- xxxxx100 [2 bytes] [ 0; 2^21-1 ]
- .
- .
- x1000000 [6 bytes] [ 0; 2^49-1 ]
- 10000000 [7 bytes] [ 0; 2^56-1 ]
- 00000000 [8 bytes] [ 0; 2^64-1 ]
- int enc(unsigned char *result, unsigned long long int value) {
- int nUsedBits = 64 - __builtin_clzll(value|1);
- int nBytesNeeded = (nUsedBits+6)/7;
- if (nBytesNeeded<9) {
- value = ((value<<1)|1)<<(nBytesNeeded-1);
- } else {
- *result++ = 0;
- }
- __builtin_memcpy(result, &value, 8);
- return nBytesNeeded;
- }
Add Comment
Please, Sign In to add comment