VB.NET分配二进制常量 [英] VB.NET Assigning a binary constant

查看:374
本文介绍了VB.NET分配二进制常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有指定一个二进制值到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屋!

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