l*******r 发帖数: 511 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: littlemar (小火星), 信区: JobHunting
标 题: 看一道面试题
发信站: BBS 未名空间站 (Mon Sep 28 20:36:19 2009, 美东)
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)
这个题是什 | q***q 发帖数: 3356 | 2 可以做乘除法么?
bit
【在 l*******r 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: littlemar (小火星), 信区: JobHunting : 标 题: 看一道面试题 : 发信站: BBS 未名空间站 (Mon Sep 28 20:36:19 2009, 美东) : 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
| z**k 发帖数: 629 | 3 是问"这道题是什么意思?"还是问"这道题该怎么解?" | g*******y 发帖数: 1930 | 4 bitwise operator:
say A=1;B=3;
a = ~0;
a<<(B-A); // a= 111...1100
a = ~a; // a= 000...00011;
a<
N = N & ~a; // N &1111..11001; clear bitA~bitB in N;
N = N | (k< |
|