range encoding algorithm