Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 给定一个二进制数,如何在O(1)时间内从右边找到第n个集合位?_Algorithm_Bit Manipulation - Fatal编程技术网

Algorithm 给定一个二进制数,如何在O(1)时间内从右边找到第n个集合位?

Algorithm 给定一个二进制数,如何在O(1)时间内从右边找到第n个集合位?,algorithm,bit-manipulation,Algorithm,Bit Manipulation,假设x=1110(12月14日),我想从右边找到第二个设置位0100(12月4日) 还有一个例子,假设x=10110010(十进制为178),我想要从右边开始的第三个设置位,即00100000(十进制为32) 如何找到它?有黑客吗?从数字中减去一将清除设置的最低有效位,同时将位设置在该位以下。与原始编号进行ANDing运算后,除原始最低设置位清除外,将保留一个与原始编号相等的编号。此过程可迭代N次,以产生一个具有最低N个清除位的数字。第n次迭代更改的位(如果有)将是原始设置的第n个最低位。从数字

假设x=1110(12月14日),我想从右边找到第二个设置位0100(12月4日)

还有一个例子,假设x=10110010(十进制为178),我想要从右边开始的第三个设置位,即00100000(十进制为32)


如何找到它?有黑客吗?

从数字中减去一将清除设置的最低有效位,同时将位设置在该位以下。与原始编号进行ANDing运算后,除原始最低设置位清除外,将保留一个与原始编号相等的编号。此过程可迭代N次,以产生一个具有最低N个清除位的数字。第n次迭代更改的位(如果有)将是原始设置的第n个最低位。

从数字中减去一将清除设置的最低有效位,同时将位设置为低于该位。与原始编号进行ANDing运算后,除原始最低设置位清除外,将保留一个与原始编号相等的编号。此过程可迭代N次,以产生一个具有最低N个清除位的数字。第n次迭代更改的位(如果有)将是原始设置的第n个最低位。

假设称为数字的2的补码符号32位整数是输入(因此只计算for循环中的0到30位):

int number=(1>i)&1;
如果(位==1)
{
++当前LSB计数;
}
if(currentLsbCount==desiredLsbCount)
{

foundLsb=number&(1假设称为number的2的补码有符号32位整数是输入(因此只计算for循环中的0到30位):

int number=(1>i)&1;
如果(位==1)
{
++当前LSB计数;
}
if(currentLsbCount==desiredLsbCount)
{

在VB.NET中,我可能会执行以下操作:

Private Function ReturnBit(input As Long, num As Long) As Long
    Dim iResult As Long = 0    'Counts set bits.
    Dim work As Long = input   'Working copy of input.

    'Looping from the LSB to the MSB of a byte. Adjust for desired 
    'length, 15 for 2 bytes, 31 for 4 bytes, etc.
    For i As Integer = 0 To 7
        'If the working variable is 0, the input does not contain as
        'many set bits as required. Return -1 if you wish.
        If work = 0 Then Return 0

        'Add the now LSB if 1, 0 otherwise. 
        iResult += (work And 1)

        'iResult contains the number of set bits now. If this is
        'the requested number, return this number. If you're just after
        'the position, just return i instead. Instead of 2^i it could be
        'more efficient to use 1<<i, but I'd rely on the compiler for
        'this.
        If iResult = num Then Return CLng(2 ^ i)

        'Remove the LSB from the working copy.
        work >>= 1
    Next
    Return 0    'Not enough set bits in input.
End Function
Private Function ReturnBit(输入为Long,num为Long)为Long
Dim iResult,因为Long=0'计数设置位。
Dim work As Long=输入的工作副本。
'从LSB循环到字节的MSB。请根据需要进行调整
'长度,15表示2个字节,31表示4个字节,等等。
对于i,整数=0到7
'如果工作变量为0,则输入不包含as
'根据需要设置多个位。如果愿意,请返回-1。
如果work=0,则返回0
'如果为1,则添加现在的LSB,否则添加0。
iResult+=(工作和1)
'iResult现在包含设置位数。如果
'请求的号码,返回此号码。如果您刚刚
“这个位置,只需返回i即可。它可能不是2^i

'要在VB.NET中更有效地使用1,我可能会执行以下操作:

Private Function ReturnBit(input As Long, num As Long) As Long
    Dim iResult As Long = 0    'Counts set bits.
    Dim work As Long = input   'Working copy of input.

    'Looping from the LSB to the MSB of a byte. Adjust for desired 
    'length, 15 for 2 bytes, 31 for 4 bytes, etc.
    For i As Integer = 0 To 7
        'If the working variable is 0, the input does not contain as
        'many set bits as required. Return -1 if you wish.
        If work = 0 Then Return 0

        'Add the now LSB if 1, 0 otherwise. 
        iResult += (work And 1)

        'iResult contains the number of set bits now. If this is
        'the requested number, return this number. If you're just after
        'the position, just return i instead. Instead of 2^i it could be
        'more efficient to use 1<<i, but I'd rely on the compiler for
        'this.
        If iResult = num Then Return CLng(2 ^ i)

        'Remove the LSB from the working copy.
        work >>= 1
    Next
    Return 0    'Not enough set bits in input.
End Function
Private Function ReturnBit(输入为Long,num为Long)为Long
Dim iResult,因为Long=0'计数设置位。
Dim work As Long=输入的工作副本。
'从LSB循环到字节的MSB。请根据需要进行调整
'长度,15表示2个字节,31表示4个字节,等等。
对于i,整数=0到7
'如果工作变量为0,则输入不包含as
'根据需要设置多个位。如果愿意,请返回-1。
如果work=0,则返回0
'如果为1,则添加现在的LSB,否则添加0。
iResult+=(工作和1)
'iResult现在包含设置位数。如果
'请求的号码,返回此号码。如果您刚刚
“这个位置,只需返回i即可。它可能不是2^i

“更有效地使用1我认为这是第n个最有意义的位,而不是最少的@ColonelThirtyTwo和James Hardy…谢谢..我已经正确地解释了..如果它不再有效,请删除您的注释..@ColonelThirtyTwo如果您开始在1@ColonelThirtyTwo..我想我很清楚了。通过“设定”位,它被理解为1而不是0。因此,x=10110010右侧的第三个设置位是10>11我认为这是第n个最重要的位,而不是Lo@ColonelThirtyTwo和James Hardy…谢谢..我已经正确地解释了..如果它不再有效,请删除您的注释..@ColonelThirtyTwo如果您开始在1@Colonel三十二..我想我很清楚。通过“Set”位,它被理解为1而不是0。因此,x=10110010右侧的第三个Set位是10>11我是否正确捕获了您的步骤?让我们从x=n
右侧获取所需的位集,而(n--)num=x&(x-1)
清除最右侧的位将是
x&=x-1;
[或者,如果首选,
x=x&(x-1);
,但在任何情况下都不使用
num
),因此清除最右边的n位将是
而(n--)x&=x-1;
。但是,如果要清除最后一位的值,则必须清除n-1位,然后保存x,然后清除下一位。差值将是新清除位的值。我是否正确捕获了步骤?让我们从x=n
的右侧设置位,同时(n--)num=x&(x-1)
清除最右边的位将是
x&=x-1;
[或者,如果愿意,
x=x&(x-1);
,但在任何情况下都不使用
num
),因此清除最右边的n位将是
而(n--)x&=x-1;
。但是,如果要清除最后一位的值,则必须清除n-1位,然后保存x,然后清除下一位。差值将是新清除位的值。@greybeard,我是一个循环,检查字节的LSB…MSB位。对于较长的输入,计数可以适当增加。2个字节构成一个循环UShort,4字节一个UInteger,8字节一个ULong。参数可以缩短一点,但我不知道OP想要这个函数做什么。在代码中添加了注释。如果使用
do…Loop
而不是
for
-Loop?@greybeard,当然也可以,但别忘了增加
>我
在循环末端旁边。这是
Private Function ReturnBit(input As Long, num As Long) As Long
    Dim iResult As Long = 0    'Counts set bits.
    Dim work As Long = input   'Working copy of input.

    'Looping from the LSB to the MSB of a byte. Adjust for desired 
    'length, 15 for 2 bytes, 31 for 4 bytes, etc.
    For i As Integer = 0 To 7
        'If the working variable is 0, the input does not contain as
        'many set bits as required. Return -1 if you wish.
        If work = 0 Then Return 0

        'Add the now LSB if 1, 0 otherwise. 
        iResult += (work And 1)

        'iResult contains the number of set bits now. If this is
        'the requested number, return this number. If you're just after
        'the position, just return i instead. Instead of 2^i it could be
        'more efficient to use 1<<i, but I'd rely on the compiler for
        'this.
        If iResult = num Then Return CLng(2 ^ i)

        'Remove the LSB from the working copy.
        work >>= 1
    Next
    Return 0    'Not enough set bits in input.
End Function