Guest User

Untitled

a guest
Nov 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. First Byte following can encode values
  2. bytes between
  3. xxxxxxx1 - [ 0; 2^ 7-1 ] // xxxx contains low bits of value
  4. xxxxxx10 [1 byte] [ 0; 2^14-1 ]
  5. xxxxx100 [2 bytes] [ 0; 2^21-1 ]
  6. .
  7. .
  8. x1000000 [6 bytes] [ 0; 2^49-1 ]
  9. 10000000 [7 bytes] [ 0; 2^56-1 ]
  10. 00000000 [8 bytes] [ 0; 2^64-1 ]
  11.  
  12. int enc(unsigned char *result, unsigned long long int value) {
  13. int nUsedBits = 64 - __builtin_clzll(value|1);
  14. int nBytesNeeded = (nUsedBits+6)/7;
  15. if (nBytesNeeded<9) {
  16. value = ((value<<1)|1)<<(nBytesNeeded-1);
  17. } else {
  18. *result++ = 0;
  19. }
  20. __builtin_memcpy(result, &value, 8);
  21. return nBytesNeeded;
  22. }
Add Comment
Please, Sign In to add comment