Mistakes

  • Bit Manipulation, always thinking about the case of negative numbers as input
  • n and m are 32-bit integers
    • n can be smaller than m!!!!!! e.x. [35, 41, 3, 9]
    • n and m can be negative numbers
  • Don't forget to clear the bit, since all bits between i and j should be equal to m!!!!
public int updateBits(int n, int m, int i, int j) {
        // write your code here

        for (int x = i; x <= j; ++x) {
            int mask = (1 << x);
            if (((1 << (x - i)) & m) != 0) {
                n |= mask;
            } else {
                n &= ~mask;
            }
        }

        return n;
    }

results matching ""

    No results matching ""