Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Extension method: void WriteInt32Optim(this Stream, int)

Declaration

public static void WriteInt32Optim(
    this Stream stream,
    int val
)

Summary

Encodes a 32-bit signed integer in a variable number of bytes, using fewer bytes for values closer to zero.

Remarks

Writes an integer 7 bits at a time. This allows small integers to be stored in 1 byte, longer ones in 2, at the cost of storing the longest ones in 5 bytes.

Example for a positive int:

00000000 00000000 01010101 01010101

becomes three bytes:

1,1010101 1,0101010 0,0000001

Example for a negative int:

11111111 11111111 11010101 01010101

becomes three bytes:

1,1010101 1,0101010 0,1111111

Note how an extra byte is needed in this example. This is similar to requiring a sign bit, however this way the positive values are directly compatible with unsigned Optim values.