c++ implementation of 5_8