Gray Code
A Gray code is a binary numbering system where two successive values differ only in one bit position. Each value in an N-bit Gray code has N bits. The most obvious case is for N = 1, when the Gray code values are just 0 and 1.
If we have a list of the N-bit Gray code values, then the N+1-bit Gray code can be obtained relatively simply. List the existing values in a column, draw a line under the last value, and "reflect" the existing values below the line. That is, duplicate the list in the opposite order. Then add a 0 bit in front of the original values and a 1 bit in front of the new "reflected" values. (Incidentally, this is why the Gray code is also called the "reflected binary code.")
For example, here is an illustration showing how we might generate the Gray code for N = 2:
So the list of Gray codes for N = 2 is 00, 01, 11, and 10.
Here is how we might generate the Gray code for N = 3:
Your task in this problem is to compute and display only the k-th value in the N-bit Gray code, suitably labeled. Assume the values are numbered starting with 0. So if N = 3 and k = 4 your program will display 110.
The Java BigInteger class may not be used in a solution to this problem.
Input
There will be multiple input cases. For each case, the input will consist of a single line containing integer values for N and k separated by one or more blanks and followed by an end of line character. N will be between 1 and 72, and k will be between 0 and 2^N−1. The line following the input line for the last case will contain two zeroes.
If it isn't immediately obvious from the preceding text, note that the value of k may not fit in a 32 bit or even a 64bit integer. It should also be noted that you will not want to compute all 2^N gray code values, but focus your attention on the computation of only the k-th such value.
Output
For each input case display the case number (1, 2, …), the values of N and k, and the k-th Gray code value (labeled as "g'). Follow the format shown in the samples below.