Cracking the coding interview--Q5.1
2014-02-15 21:45
246 查看
题目
原文:
You are 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:
Input: N = 10000000000, M = 10101, i = 2, j = 6
Output: N = 10001010100
译文:
给两个32位的数,N和M,和两个指示位的数,i和j。写一个方法使N中的第i到j位的值等于M(即M成为N的子串且位于N的第i到j位之间)
例如:
输入:N=10000000000, M = 10101, i = 2, j = 6
输出:N=10001010100
解答
方案1:用一个左边全为1,中间一段全为0(这段的长度与m长度一样), 右边全为1的掩码mask去和n按位与,得到的值是将n中间一段清0的结果。 然后再与m左移i位后按位或,得到最终结果。
代码如下:
方案2:先将N中第0位到第i位保存下来(左闭右开:[0, i)),记作ret, 然后将N中第0位到第j位全清0([0, j]),通过向右移动j+1位然后再向左移动j+1位得到。 最后用上面清0后的值或上(m<<i)再或上ret即可。
代码如下:
完整代码如下:
---EOF---
原文:
You are 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:
Input: N = 10000000000, M = 10101, i = 2, j = 6
Output: N = 10001010100
译文:
给两个32位的数,N和M,和两个指示位的数,i和j。写一个方法使N中的第i到j位的值等于M(即M成为N的子串且位于N的第i到j位之间)
例如:
输入:N=10000000000, M = 10101, i = 2, j = 6
输出:N=10001010100
解答
方案1:用一个左边全为1,中间一段全为0(这段的长度与m长度一样), 右边全为1的掩码mask去和n按位与,得到的值是将n中间一段清0的结果。 然后再与m左移i位后按位或,得到最终结果。
代码如下:
public static int update_bits(int n,int m,int i,int j){ int max=~0;//全为1 int left=max-((1<<j+1)-1); int right=((1<<i)-1); int mask=left|right; return (n&mask)|(m<<i); }
方案2:先将N中第0位到第i位保存下来(左闭右开:[0, i)),记作ret, 然后将N中第0位到第j位全清0([0, j]),通过向右移动j+1位然后再向左移动j+1位得到。 最后用上面清0后的值或上(m<<i)再或上ret即可。
代码如下:
public static int update_bits1(int n,int m,int i,int j){ int ret=(1<<i)-1; ret&=n; return ((n>>(j+1))<<(j+1))|(m<<i)|ret; }
完整代码如下:
class Q5_1{
public static void print_binary(int n){
String binaryNum="";
while(n>=1){
binaryNum+=n%2;
n=n/2;
}
StringBuffer num = new StringBuffer(binaryNum);
System.out.println(num.reverse().toString());
}
//方法一
public static int update_bits(int n,int m,int i,int j){ int max=~0;//全为1 int left=max-((1<<j+1)-1); int right=((1<<i)-1); int mask=left|right; return (n&mask)|(m<<i); }
//方法二
public static int update_bits1(int n,int m,int i,int j){ int ret=(1<<i)-1; ret&=n; return ((n>>(j+1))<<(j+1))|(m<<i)|ret; }
public static void main(String[] args){
int n=1<<10,m=21;
int ans = update_bits(n, m, 2, 6);
print_binary(n);
print_binary(m);
print_binary(ans);
}
}
---EOF---
相关文章推荐
- Cracking the coding interview--Q5.1
- Cracking the Coding Interview-ch7 | Object Oriented Design
- Cracking the coding interview的习题解答
- Cracking the Coding Interview 150题(一)
- 【Cracking the coding interview】Q1.3(移除重复字符)
- Cracking the coding interview--Q20.6
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
- 《Cracking the Coding Interview》——第12章:测试——题目2
- 《Cracking the Coding Interview》——第13章:C和C++——题目8
- Cracking the Coding Interview Q3.1
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1
- 《Cracking the Coding Interview》——第17章:普通题——题目6
- 《Cracking the Coding Interview》——第18章:难题——题目4
- Cracking The Coding Interview 3rd -- 1.2
- Cracking the coding interview--Q8.4
- Cracking the coding interview--Q9.5
- Cracking the coding interview--Q1.7
- Cracking the coding interview--Q2.1
- cracking the coding interview problem solution 1.8
- Cracking the coding interview--Q1.8