improved binary to graycode conversion