How to convert decimal to Variable Byte code & gamma code

Adam Lynch picture Adam Lynch · Dec 13, 2011 · Viewed 11.3k times · Source

How do you convert the decimal number 777 to the equivalent VB & gamma codes?

I've been reading up on gamma codes. I see where they get the unary codes from the decimal but not where the length & offset comes from. I also understand that the gamma code is just the length (of the unary code) concatenated with the offset.

Answer

Ben.K picture Ben.K · Jan 2, 2015

777 in binary code is 1100001001

Gamma code

  • calculate offset: remove the first 1 is 100001001
  • calculate length: how many bit of offset(9 bits) in unary code 1111111110 (nine 1s and one 0)
  • put them together 1111111110100001001

VB code

  • Get the last seven bits from the binary code 1100001001 is 0001001, add 1 as the "head" bit (0001001 -> 10001001) because there are still 3 bits left in the original binary code.
  • Get the remain 3 bits, this time use 0 as the "head" bit (110 -> 00000110) because there is no remainder in the original binary code
  • put these two bytes together 0000011010001001 is the VB code.

In esence, VB code splits the gap (in binary) into 7 bit partitions and set the continuation bit/1st bit of last/right most 7 bits part to 1 and all other parts's continuation bit to 0.