本文介绍了我如何在C#中使用二进制运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是新来的C#.这里有一些来自VB6.0的代码.
我想将其转换为C#,任何机构都可以帮助我.

Hi to all,

I am new i C#. Here i have some line of code from VB6.0.
I want to convert it into c#,can any body can help me how can i do this.

Dim i As Integer
    List1.Clear
    For i = 0 To 6
        If Text1.Text And 2 ^ (i - 1) Then
            List1.AddItem (i)
        Else
            List2.AddItem (i)
        End If
    Next i




在这里,我尝试将其转换为C#,但它与VB6.0中的工作方式不同.





here i tried to convert it into c# but it is not working same as it was working in VB6.0.


for (int i = 0; i < 6; i++)
        {
            if (Convert.ToBoolean(int.Parse(TextBox1.Text) & (2 ^ (i - 1))))
                Response.Write("<br>" + (i));
        }




按位``AND''运算符(&):
比较两个数字的位,并返回每个数字都为1且两个数字都为1的数字.
但是在这里我没有得到正确的结果.
示例:0001 1010& amp; 0000 1000 = 0000 1000

我想得到这样的结果
文字输入= 3
输出结果:1,2

文字输入:6
输出结果:2,3

文字输入:15 ​​
输出结果:1,2,3,4

预先感谢...




Bitwise ''AND'' operator (&):
compares the bits of two numbers, and return a number with a 1 in every bit that has a 1 in both numbers.
but here i am not getting the correct result.
Example: 0001 1010 & 0000 1000 = 0000 1000

I want to get result like this
text input = 3
output result :1,2

text input :6
output result : 2,3

text input :15
output result :1,2,3,4

Thanks in advance...

推荐答案

if (Convert.ToBoolean(int.Parse(TextBox1.Text) && 2 ^ (i - 1)))



顺便说一句:您期望什么样的文字?将文本框解析为int充满了问题(用户就是他们的真实身份),而没有将其转换为bool.最好以其他方式进行测试,但是在不知道您期望的输入种类的情况下,我无法告诉您什么.



BTW: What kind of text are you expecting? Parsing a textbox to an int is fraught with problems as it is (users being what they are) without converting that to a bool. It might be better to do the test a different way, but I can''t tell you what without knowing the kind of input you expect.


double c=0;
int d;
for (int i = 1; i < 6; i++)
{
    c = Math.Pow(2, (i - 1));
    d = int.Parse(c.ToString());
    if (Convert.ToBoolean(int.Parse(TextBox1.Text) & d))
        Response.Write("<br>" + (i));
}</br>


这篇关于我如何在C#中使用二进制运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:00