http://www.lintcode.com/en/problem/update-bits/
Way 1: single bit set
http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
Check the bit of M is 1 or 0, if 1, set the corresponding bit of M as 1, else, set 0.
Time: O(j - i) , j - i < 32, so it can be regarded as O(1)
Space: O(1)
First, we need a mask, which is 0 from i to j, and 1 for other bits;
Then N & mask, set bits from i to j as 0;
Now we bitwise shift left m by i bits
At last, just bitwise or (N & mask) and shifted m. (set 1)
Another version, the mask is 1 from i to j, and 0 for other bits;
Then N or mask, set bits from i to j as 1;
Now we bitwise shift left m by ibits
At last, just bitwise & (N | mask) and shifted m. (set 0)
The key is how to get mask?
We can easily get the left part of mask by left shift. However, we cannot directly use right shift the same way as we get right part. Since for signed int, the logical shift and arithmetic shift works differently.
http://blog.csdn.net/hengshan/article/details/6440549(explain how left and right shift works)
From the paragraph above, we know that the logical shift is caused by signed bit.
In this problem, bits >= i will be set to 0, however, bit <= 31 for input.
So for right part, bits from i to 31 will be zero. So the bit[31] is always 0. Which means the signed bit of right part is always 0. Then I can use INT_MAX as the initial right part value than just shift right, which will only add 0 from the left side.
Time: O(1)
Space: O(1)
my version(both left and right shift):
Common version: ( only use left shift)
The right part in an Integer than from 31 to i bit is zero, and from i - 1 to 0 bit is 1
Beside right shift INT_MAX, we can also left shift 1 to i bit, then minus 1.
http://algorithm.yuanbin.me/math_and_bit_manipulation/update_bits.html
Time: O(1)
Space: O(1)
KEY: 0 | x = x
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)
Example
Given
N=(10000000000)2
, M=(10101)2
, i=2
, j=6
return
N=(10001010100)2
Note
In the function, the numbers N and M will given in decimal, you should also return a decimal number.
Challenge
Minimum number of operations?
Clarification
http://www.jiuzhang.com/solutions/update-bits/
You can assume that the bits j through i have enough space to fit all of M. That is, if M=10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.
int updateBits(int n, int m, int i, int j) { int mask; if (j < 31) { mask = ~((1 << (j + 1)) - (1 << i)); } else { mask = (1 << i) - 1; } return (m << i) + (mask & n); }http://blog.welkinlan.com/2015/05/26/724/
1. Make a & mask to transform N[i…j] to 0.
2. Add M to N[i…j].
Way 1: single bit set
http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
Check the bit of M is 1 or 0, if 1, set the corresponding bit of M as 1, else, set 0.
Time: O(j - i) , j - i < 32, so it can be regarded as O(1)
Space: O(1)
int updateBits(int n, int m, int i, int j) { for(; i <= j; i++){ if(m & 1) n = n | (1<<i); else n = n & ~(1<<i); m = m>>1; } return n; }
n = n & ~(1 << k); 就是把n中的第k个bit设为0
n = n | ((m & (1<<(k-i)))<<i);
- m & (1<<(k-i)) 是从m的第一个bit开始扫描.因为已知j-i = size of (m)2
- ((m & (1<<(k-i)))<<i) 扫描后, 往左shift i位对准n上的i位.
- n = n | ((m & (1<<(k-i)))<<i) 把n的第i位到j位设为m的0~(j-i)位
public int updateBits(int n, int m, int i, int j) {
// write your code here
for(int k = i ; k <= j; k++) {
n = n & ~(1 << k); // set kth bit in n to 0
n = n | ((m & (1<<(k-i)))<<i); //set kth bit in n to m's kth bit
}
return n;
}
Way 2: several bits set togetherFirst, we need a mask, which is 0 from i to j, and 1 for other bits;
Then N & mask, set bits from i to j as 0;
Now we bitwise shift left m by i bits
At last, just bitwise or (N & mask) and shifted m. (set 1)
Another version, the mask is 1 from i to j, and 0 for other bits;
Then N or mask, set bits from i to j as 1;
Now we bitwise shift left m by ibits
At last, just bitwise & (N | mask) and shifted m. (set 0)
The key is how to get mask?
We can easily get the left part of mask by left shift. However, we cannot directly use right shift the same way as we get right part. Since for signed int, the logical shift and arithmetic shift works differently.
http://blog.csdn.net/hengshan/article/details/6440549(explain how left and right shift works)
From the paragraph above, we know that the logical shift is caused by signed bit.
In this problem, bits >= i will be set to 0, however, bit <= 31 for input.
So for right part, bits from i to 31 will be zero. So the bit[31] is always 0. Which means the signed bit of right part is always 0. Then I can use INT_MAX as the initial right part value than just shift right, which will only add 0 from the left side.
Time: O(1)
Space: O(1)
my version(both left and right shift):
int updateBits(int n, int m, int i, int j) { // write your code here int left = ~0 << (j + 1) ; if(j >= 31) left = 0; //for signed int, left shift is logical shift; // while right shift is arithmetic shift. // so we cannot directly use right shift to get mask; //int right = ~0 >> (32 - i); int right = INT_MAX; right = right >> (31 - i); int mask = left | right; return m<<i | (mask & n); }
Common version: ( only use left shift)
The right part in an Integer than from 31 to i bit is zero, and from i - 1 to 0 bit is 1
Beside right shift INT_MAX, we can also left shift 1 to i bit, then minus 1.
http://algorithm.yuanbin.me/math_and_bit_manipulation/update_bits.html
Time: O(1)
Space: O(1)
int updateBits(int n, int m, int i, int j) { // write your code here int left = ~0 << (j + 1); if( j == 31) left = 0; int right = (1 << i) - 1;//Don't forget parenthesis int mask = left | right; return (n & mask) | (m<<i); }