VB.NET分配二进制常量 [英] VB.NET Assigning a binary constant
问题描述
有没有指定一个二进制值到VB变量的方法吗?所有明显的选择不工作。我试过prefixing与和B
与 B追加
,但似乎没有任何工作。我没有多少运气要么寻找它。我不需要这个我的应用程序,但我宁愿,只是好奇,所以备用解决方案是没有必要的。
Is there a way to assign a binary value to a vb variable? All of the obvious choices don't work. I've tried prefixing with &B
, appending with b
but nothing seems to work. I'm not having much luck searching for it either. I don't NEED this for my application, but am rather, just curious so alternate solutions are not necessary.
对于澄清,我一直在寻找(这似乎并不可能)是如何字面二进制数分配给一个变量,以类似的方式中,你可以指定一个十六进制或八进制数。我只是在寻找一个更直观吸引人的方式赋值给一个标志枚举。
For clarification, what I was looking for (which does not appear to be possible) was how to assign a literal binary number to a variable, in a similar manner in which you can assign a hex or octal number. I was just looking for a more visually engaging way to assign values to a flag enum.
Dim num as Integer = &H100ABC 'Hex'
Dim num2 as Integer = &O123765 'Octal'
Dim myFantasy as Integer = &B1000 'What I would like to be able to do'
Dim myReality as Integer = 2 ^ 3 'What I ended up doing'
谢谢!
推荐答案
我可能这一个后捅死自己:
I might stab myself after this one:
Convert.ToInt32("1100101", 2);
在一个更严重的(注意到,在更新的问题),对标志枚举
你可能想什么的向左移位运算符(<<
):
On a more serious note (noticing that you have updated the question), for flag enums
what you may want is the left shift operator (<<
):
Dim myReality as Integer = 1 << 0 // 1
Dim myAlternateReality as Integer = 1 << 1 // 2
Dim myParallelUniverse as Integer = 1 << 2 // 4
...
等多达31个。
这篇关于VB.NET分配二进制常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!