<s>[INST] write a java function that implements gray code and returns the next sequence code after a given code in a given direction of rotation [/INST] ```
public static int getNextGrayCode(int num, boolean direction) {
    if (direction) {
        return num ^ (num >> 1);
    } else {
        int mask = num;
        while ((mask & 1) == 0) {
            mask >>= 1;
        }
        return num ^ mask | (mask >> 1);
    }
}
```</s>\ <s>[INST] Could you describe how this code works? [/INST] uh</s>\ <s>[INST] Did you write code without understanding it? [/INST] 