Encoder and decoder for binary stream