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

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

问题描述

是否可以将二进制值分配给VB变量?所有明显的选择都不起作用。我尝试使用& 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 I'm 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。

and so on up to 31.

这篇关于分配二进制常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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