二进制问题 [英] Binary question
问题描述
给定任意字节(或者就此而言是int),有没有办法计算
out:
1)一个'套数的*数没有循环?也许是一些公式?
2)从右边看,如果所有1'都是连续的,而不是
干预0'。 0'结束了确定。
TIA
-
William Stacey,MVP
Given any arbitrary byte (or int for that matter), is there a way to figure
out:
1) The *number of one''s set without a loop? Maybe some formula?.
2) Looking from left the right, if all 1''s are continuous with not
intervening 0''s. 0''s on the end ok.
TIA
--
William Stacey, MVP
推荐答案
" William Stacey" < ST *********** @ mvps.org>写道:
"William Stacey" <st***********@mvps.org> wrote:
给定任意字节(或int为此),有没有办法让
弄清楚:
1)没有任何一个'一个循环?也许是一些公式?。
2)从左边看,如果所有1'都是连续的,而不是介入0'。 0'结束了确定。
Given any arbitrary byte (or int for that matter), is there a way to figure out:
1) The *number of one''s set without a loop? Maybe some formula?.
2) Looking from left the right, if all 1''s are continuous with not
intervening 0''s. 0''s on the end ok.
我会在编译时使用一个填充了所需信息的数组
时间,并且字节作为运行时数组的索引。
Jens。
-
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
用我的第一个替换MSDN回复我的电子邮件地址时的名字!
I would use an array that is filled with the required information at compile
time, and the byte as index to the array at run-time.
Jens.
--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!
是的我能做到这一点并感谢您的回复。我只是在想这个
可以用数学来完成。
-
-
William Stacey,MVP
" Jens Thiel" < MS ** @ Thiel.de>在消息中写道
新闻:#D ************* @ TK2MSFTNGP10.phx.gbl ...
Yes I could do that and thanks for the reply. I was just thinking this
could be done with math.
--
--
William Stacey, MVP
"Jens Thiel" <MS**@Thiel.de> wrote in message
news:#D*************@TK2MSFTNGP10.phx.gbl...
" William Stacey" < ST *********** @ mvps.org>写道:
"William Stacey" <st***********@mvps.org> wrote:
给定任意字节(或int为此),有没有办法计算
Given any arbitrary byte (or int for that matter), is there a way to figure
out:
1)* number of one'没有循环的设置?也许是一些公式?。
2)从左边看,如果所有1'都是连续的,而不是介入0'。 0'结束了确定。
out:
1) The *number of one''s set without a loop? Maybe some formula?.
2) Looking from left the right, if all 1''s are continuous with not
intervening 0''s. 0''s on the end ok.
我会在
I would use an array that is filled with the required information at
编译时使用一个填充了所需信息的数组,并且字节作为运行时数组的索引。
Jens。
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
在回复时,用我的名字替换MSDN我的电子邮件地址!
compile time, and the byte as index to the array at run-time.
Jens.
--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!
仅供参考。做了一些测试和研究,并想出了这个。任何
优化?
///< summary>
///返回1的数字在任何uint中设置的位。
///< / summary>
public static int BitCount(uint x)
{
int bitCount = 0;
if(x!= 0)
{
do
{
bitCount ++;
x = x& (x - 1);
} while(x!= 0);
}
返回bitCount;
}
///< summary>
///给定任何uint,如果1位从左边连续到
right。
///此方法也将返回out参数1位的数量
in
/// num,即使这些位不是连续的。
///< / summary>
public static bool ContiguousBits(uint num,out int bitCount)
{
int shift = 0;
int bc = 0;
bc = Bits.BitCount(num); //上面的BitCount方法。
shift = 32 - bc;
bitCount = bc;
return((num>> shift )==(Math.Pow(2,bc) - 1));
}
-
William Stacey ,MVP
" William Stacey" < ST *********** @ mvps.org>在消息中写道
news:uL ************* @ tk2msftngp13.phx.gbl ...
FYI. Did some more testings and research and came up with this. Any
optimizations?
/// <summary>
/// Returns the number of "1" bits set in any uint.
/// </summary>
public static int BitCount(uint x)
{
int bitCount = 0;
if ( x != 0)
{
do
{
bitCount++;
x = x & (x - 1);
} while (x != 0);
}
return bitCount;
}
/// <summary>
/// Given any uint, returns true if the 1 bits are contiguous from left to
right.
/// This method will also return to the "out" parameter the number of 1 bits
in
/// the num, even if the bits are not contiguous.
/// </summary>
public static bool ContiguousBits(uint num, out int bitCount)
{
int shift = 0;
int bc = 0;
bc = Bits.BitCount(num); //The BitCount Method above.
shift = 32 - bc;
bitCount = bc;
return ( (num >> shift) == (Math.Pow(2, bc) - 1) );
}
--
William Stacey, MVP
"William Stacey" <st***********@mvps.org> wrote in message
news:uL*************@tk2msftngp13.phx.gbl...
给定任意字节(或对于那个问题,有没有办法让
弄明白:
1)没有循环的一个'套数?也许是一些公式?。
2)从左边看,如果所有1'都是连续的,而不是介入0'。 0'结束了确定。
TIA
-
William Stacey,MVP
Given any arbitrary byte (or int for that matter), is there a way to figure out:
1) The *number of one''s set without a loop? Maybe some formula?.
2) Looking from left the right, if all 1''s are continuous with not
intervening 0''s. 0''s on the end ok.
TIA
--
William Stacey, MVP
这篇关于二进制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!