二进制BitString到Int32:只是位小提琴手!你能提高吗? [英] Binary BitString to Int32 : Bit fiddlers only! Can you improve this?

查看:91
本文介绍了二进制BitString到Int32:只是位小提琴手!你能提高吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这个程序将BitString转换为0101110。 Int32值。

任何人都可以改进或者缩短以下程序吗?


public static int BitStringToInt32(String s)

{

int j,k,m = 0;

int len = s.Length - 1;


for (int i = 0; i< = len; i ++)

{

j = 1;

k =(s [i] - ' '0'');


if((k 1)||(k <0))

{

抛出新的System.IO.InvalidDataException(输入无效。必须为0或

1);

}


j = j<< (len - i);

m = m + k * j;

}


返回m;

}


谢谢

Russell Mangel

拉斯维加斯,NV

解决方案

" Russell Mangel" < ru ***** @ tymer.netwrote:


任何人都可以改进或缩短以下程序吗?



public static int BitStringToInt32(String s)

{

return Convert.ToInt32(s,2) ;

}


Eq。


Russell Mangel< ru ***** @ tymer.netwrote:


这个程序将BitString转换为0101110。 Int32值。

任何人都可以改进或缩短以下程序吗?



当然:


Convert.ToInt32(s,2);


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复群组,请不要给我发邮件


Russell Mangel写道:





此程序转换BitString0101110 ; Int32值。

任何人都可以改进或者缩短以下程序吗?


public static int BitStringToInt32(String s)

{

int j,k,m = 0;

int len = s.Length - 1;


for (int i = 0; i< = len; i ++)

{

j = 1;

k =(s [i] - ' '0'');


if((k 1)||(k <0))

{

抛出新的System.IO.InvalidDataException(输入无效。必须为0或

1);

}


j = j<< (len - i);

m = m + k * j;

}


返回m;

}


谢谢

Russell Mangel

拉斯维加斯,NV



忽略转换的明显用法,或许这更像是你想要的b
$ b public static int BitStringToInt32(String s){

int result = 0;

foreach(char c in s){

if(c!=''0''&& ; c!=''1''){

抛出新的ArgumentException(输入无效。必须为0或1。;

}

结果<< = 1;

结果+ = c& 1;

}

返回结果;

}


-

G?ran Andersson

_____
http:// www.guffa.com


Hi,

This programs converts a BitString "0101110" to an Int32 value.
Can anyone improve, or possibly shorten the following program?

public static int BitStringToInt32(String s)
{
int j, k, m = 0;
int len = s.Length - 1;

for (int i = 0; i <= len; i++)
{
j = 1;
k = (s[i] - ''0'');

if ((k 1) || (k < 0))
{
throw new System.IO.InvalidDataException("Invalid input. Must be 0 or
1");
}

j = j << (len - i);
m = m + k * j;
}

return m;
}

Thanks
Russell Mangel
Las Vegas, NV

解决方案

"Russell Mangel" <ru*****@tymer.netwrote:

Can anyone improve, or possibly shorten the following program?

public static int BitStringToInt32(String s)
{
return Convert.ToInt32(s, 2);
}

Eq.


Russell Mangel <ru*****@tymer.netwrote:

This programs converts a BitString "0101110" to an Int32 value.
Can anyone improve, or possibly shorten the following program?

Sure:

Convert.ToInt32(s, 2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Russell Mangel wrote:

Hi,

This programs converts a BitString "0101110" to an Int32 value.
Can anyone improve, or possibly shorten the following program?

public static int BitStringToInt32(String s)
{
int j, k, m = 0;
int len = s.Length - 1;

for (int i = 0; i <= len; i++)
{
j = 1;
k = (s[i] - ''0'');

if ((k 1) || (k < 0))
{
throw new System.IO.InvalidDataException("Invalid input. Must be 0 or
1");
}

j = j << (len - i);
m = m + k * j;
}

return m;
}

Thanks
Russell Mangel
Las Vegas, NV

Disregarding the obvious use of Convert, perhaps this is rather what you
were expecting?

public static int BitStringToInt32(String s) {
int result = 0;
foreach (char c in s) {
if (c != ''0'' && c != ''1'') {
throw new ArgumentException("Invalid input. Must be 0 or 1.");
}
result <<= 1;
result += c & 1;
}
return result;
}

--
G?ran Andersson
_____
http://www.guffa.com


这篇关于二进制BitString到Int32:只是位小提琴手!你能提高吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆