两个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