class Solution { public: /** *@param n, m: Two integer *@param i, j: Two bit positions *return: An integer */ int updateBits(int n, int m, int i, int j) { if (i == 0 && j == 31) return m; int cnt = j - i + 1; int mask = (1 << cnt) - 1; int nn = (n >> i) & mask; n ^= (nn << i); // clear n |= (m << i); // set return n; } };
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)