l*******r 发帖数: 511 | 1 given a 32 bit number N and 2 numbers(A & B) that determine 2 different bit
pos
itions of N how do you make all the bits between A and B equal to another
given
integer k.
given (A,B is in the range [0 to 31] and
k<=2^(B-A+1) ( so that k fits between B-A+1 bits). Give an O(1) solution for
th
is
e.g if N=9 ( 1001) ,A=0 ,B=2,K=5(101 then the result should be 1101 (1.e 13)
这个题是什么意思啊? |
a**********s 发帖数: 588 | 2 If that answer is something like:
(k << A) | (((-1) << (B+1)) & N) | ((unsigned(-1) >> (32-A)) & N)
Then this is a crap 面试题... |
f**e 发帖数: 1269 | 3 hoho... 不错,读algorithmics的帖子总是痛并快乐着……
位操作读起来太痛苦了,我一看你写的跟我不一样就懒得读了。我写的是
if (k^ ((n<<(31-B)) >>> (31-B + A-1)) == 0)
then return true
【在 a**********s 的大作中提到】 : If that answer is something like: : (k << A) | (((-1) << (B+1)) & N) | ((unsigned(-1) >> (32-A)) & N) : Then this is a crap 面试题...
|
l*******r 发帖数: 511 | 4 I don't know the answer but what is given in the prob?
【在 a**********s 的大作中提到】 : If that answer is something like: : (k << A) | (((-1) << (B+1)) & N) | ((unsigned(-1) >> (32-A)) & N) : Then this is a crap 面试题...
|
f**e 发帖数: 1269 | 5 就是说你有个数n用32位表达
其中a和b分别指向两个不一样的bit,比如说a指向bit 3, b指向bit 8。
问a和b之间的binary串的值是否等于k?
【在 a**********s 的大作中提到】 : If that answer is something like: : (k << A) | (((-1) << (B+1)) & N) | ((unsigned(-1) >> (32-A)) & N) : Then this is a crap 面试题...
|
a**********s 发帖数: 588 | 6 不是, 题目要求你把k填到从A到B之间的二进制位
【在 f**e 的大作中提到】 : 就是说你有个数n用32位表达 : 其中a和b分别指向两个不一样的bit,比如说a指向bit 3, b指向bit 8。 : 问a和b之间的binary串的值是否等于k?
|
a**********s 发帖数: 588 | 7 I wrote a short program for OP to understand:
char buffer[65];
unsigned int N = 0x8fde27e3;
unsigned int A = 3, B = 17, K = 0x7893;
printf("N = %s....\n", itoa(N, buffer, 2));
printf("K = ..............%s ....\n", itoa(K, buffer, 2));
unsigned int Rk = (K << A);
unsigned int Ra = ((unsigned(-1) >> (32-A)) & N);
unsigned int Rb = ((-1 << (B+1)) & N);
unsigned int R = Ra | Rk | Rb;
printf("Ra = %s....\n", itoa(Ra, buffer, 2));
printf("Rk = %s....\n", |
f**e 发帖数: 1269 | 8 a....我看错了。
那得重写
N ^ (((N<<(31-B))>>>(31 + A - B))<
【在 a**********s 的大作中提到】 : 不是, 题目要求你把k填到从A到B之间的二进制位
|