```
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);
    }
}
```
```
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);
    }
}
```