兩個32位數字給出M和N.兩個比特位置給出i和j。該方法應該設置i和j之間的所有比特在N中等於M.位操作:修改32位整數以包含子串
N =千萬 M = 1111 I = 3且j = 7 輸出:N = 10111100
int modifybits(int i,int j,int N,int M)
{
int max=1;
//the string from the left of i and right of j should remain the same and the rest should become 0
int left= N>>31-i
left=left<<31
int right =N<<j
right=right>>31-j
int new_N=left|right
int result=new_N|M
print(result)
}
你能提供更好的解決方案,這似乎沒有在工作!!日Thnx adv
如果'j - i!= M'的長度會發生什麼? – Jon 2011-03-31 09:46:49
這不是真的有助於調用一個整數字符串。 – 2011-03-31 09:54:30