在VB中使用整数的逻辑运算符 [英] Usisng logical operators with integers in VB

查看:117
本文介绍了在VB中使用整数的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G''day all


我用bitmapping整数来存储组合的布尔值,并且点击了一点点b / b
。 />

我希望能够关闭某些位,如果它们匹配的是一个数字

但是如果它们不能打开它们(如在xor的情况。基本上我希望

使用逻辑运算A B.


例如..


AB结果

0 0 = 0

0 1 = 0

1 0 = 1

1 1 = 0

因此,如果A = 3且B = 9,则结果为2.


VB中是否存在逐位大于函数的功能?


我看着''AndAlso''功能可能会起作用但是

它只是布尔值。


非常感谢任何帮助。


干杯。


-

现实继续毁了我的生活。

G''day all

I''m bitmapping integers to store combined boolean values and have hit a bit
of a snag.

I want to be able to turn off certain bits if they they match in a number
but not turn them on if they don''t (as in the case of xor). Basically I wish
to use the logical operation A B.

For example..

A B Result
0 0 = 0
0 1 = 0
1 0 = 1
1 1 = 0

So that if A = 3 and B = 9 then result would be 2.

Is there a bit-wise greater-than function available in VB?

I was looking at the ''AndAlso'' function could possibly be made to work but
it''s boolean only.

Any help would be greatly appreciated.

Cheers.

--

Reality continues to ruin my life.

推荐答案

2008-04-29,werdan< fo ** ******@that.gmail.fad.comwrote:
On 2008-04-29, werdan <fo********@that.gmail.fad.comwrote:

G''day all


我' '是位图整数来存储组合的布尔值,并且已经点了一下

的障碍。


我希望能够关闭某些位,如果他们匹配的数字是

但是如果他们不打开就不能打开它们(如果是这样的话) of xor)。基本上我希望

使用逻辑运算A B.


例如..


AB结果

0 0 = 0

0 1 = 0

1 0 = 1

1 1 = 0

因此,如果A = 3且B = 9,则结果为2.


VB中是否存在逐位大于函数的功能?


我看着''AndAlso''功能可能会起作用但是

它只是布尔值。


非常感谢任何帮助。


干杯。
G''day all

I''m bitmapping integers to store combined boolean values and have hit a bit
of a snag.

I want to be able to turn off certain bits if they they match in a number
but not turn them on if they don''t (as in the case of xor). Basically I wish
to use the logical operation A B.

For example..

A B Result
0 0 = 0
0 1 = 0
1 0 = 1
1 1 = 0

So that if A = 3 and B = 9 then result would be 2.

Is there a bit-wise greater-than function available in VB?

I was looking at the ''AndAlso'' function could possibly be made to work but
it''s boolean only.

Any help would be greatly appreciated.

Cheers.



我想我明白了什么你试图完成 - 但是,不会是

更容易使用带有flags属性集的枚举和这样的值:


Option Strict在

选项明确的开启


进口系统

进口System.Text


模块模块1

< Flags()_

枚举选项s

选项1 =& H1

选项2 =& H2

选项3 =& H4

选项4 = & H8

Option5 =& H10

结束枚举


Public Sub Main()

Dim o As Option = Option1或Option2或Option5

PrintOptions(o)

End Sub


Sub PrintOptions(ByVal o作为选项)

Dim values()As Integer = CType([Enum} .GetValues(GetType(Options)),Integer())

Dim buffer As StringBuilder = new StringBuilder()


For Each value As Integer In values

如果CBool​​(m和值)那么

buffer.AppendFormat( " {0},",[Enum] .GetName(GetType(选项),值))

结束如果

下一页


buffer.Length = buffer.Length -1

Console.WriteLine(缓冲区)

结束子

结束模块

所以,基本上 - 你要找到一个特定的选项,你可以和

它...


如果CBool​​( o和O. ptions.Option1)然后

做东西...

结束如果


-

Tom Shelton

I think I see what your trying to accomplish - but, wouldn''t it be
easier to use an enum with the flags attribute set and values like this:

Option Strict On
Option Explicit On

Imports System
Imports System.Text

Module Module1
<Flags()_
Enum Options
Option1 = &H1
Option2 = &H2
Option3 = &H4
Option4 = &H8
Option5 = &H10
End Enum

Public Sub Main ()
Dim o As Option = Option1 Or Option2 Or Option5
PrintOptions(o)
End Sub

Sub PrintOptions (ByVal o As Options)
Dim values() As Integer = CType([Enum}.GetValues(GetType(Options)), Integer())
Dim buffer As StringBuilder = new StringBuilder()

For Each value As Integer In values
If CBool(m And value) Then
buffer.AppendFormat ("{0},", [Enum].GetName(GetType(Options), value))
End If
Next

buffer.Length = buffer.Length -1
Console.WriteLine (buffer)
End Sub
End Module
So, basically - you to find if a specific option is on, you can just and
it...

If CBool (o And Options.Option1) Then
do stuff...
End If

--
Tom Shelton




" Tom Shelton" < to ********* @ YOUKNOWTHEDRILLcomcast.netWrote in message

news:ec ************* @ TK2MSFTNGP02.phx.gbl .. 。

"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ec*************@TK2MSFTNGP02.phx.gbl...

2008-04-29,werdan< fo ******** @ that.gmail.fad.comwrote:


我想我看到了你想要完成的事情 - 但是,不会是这样吗?b $ b更容易使用带有flags属性集的枚举和类似这样的值: />

选项严格开启

选项明确开启


进口系统

进口System.Text


模块模块1

< Flags()_

枚举选项

选项1 =& H1

Option2 =& H2

Option3 =& H4

Option4 =& H8

Option5 = & H10

结束枚举


Public Sub Main()

Dim o As Option = Option1或Option2 Or Option5

PrintOptions(o)

End Sub


子PrintOptions(ByVal o As Options)

昏暗值()作为Integ er = CType([Enum} .GetValues(GetType(Options)),

Integer())


For Each value As Integer In value

如果CBool​​(m和值)那么

buffer.AppendFormat(" {0}," ,[Enum] .GetName(GetType(选项),值))

结束如果

下一页


buffer.Length = buffer.Length -1

Console.WriteLine(缓冲区)

End Sub

结束模块


所以,基本上 - 你要找到一个特定的选项,你可以和

它...


如果是CBool​​(o和选项。选项1)然后

做东西...

结束如果
On 2008-04-29, werdan <fo********@that.gmail.fad.comwrote:

I think I see what your trying to accomplish - but, wouldn''t it be
easier to use an enum with the flags attribute set and values like this:

Option Strict On
Option Explicit On

Imports System
Imports System.Text

Module Module1
<Flags()_
Enum Options
Option1 = &H1
Option2 = &H2
Option3 = &H4
Option4 = &H8
Option5 = &H10
End Enum

Public Sub Main ()
Dim o As Option = Option1 Or Option2 Or Option5
PrintOptions(o)
End Sub

Sub PrintOptions (ByVal o As Options)
Dim values() As Integer = CType([Enum}.GetValues(GetType(Options)),
Integer())
Dim buffer As StringBuilder = new StringBuilder()

For Each value As Integer In values
If CBool(m And value) Then
buffer.AppendFormat ("{0},", [Enum].GetName(GetType(Options), value))
End If
Next

buffer.Length = buffer.Length -1
Console.WriteLine (buffer)
End Sub
End Module
So, basically - you to find if a specific option is on, you can just and
it...

If CBool (o And Options.Option1) Then
do stuff...
End If



感谢Tom的回复。 />

这基本上就是我正在做的事情。我有一些枚举可以有多个选项,我将它们存储在数据库中的整数字段中。

表。我试图做的是,使用你的例子,如果新记录

选择了选项2和选项4,表中的任何其他记录都有

他们的选项2和选项4关闭。我认为VB可能有一个函数

来做它,但看起来我只需编写一个循环通过

的位并单独比较它们。 br />

再次感谢。


Thanks for the reply Tom.

That''s basically what I''m doing. I have some enumerations that can have
multiple selections and I''m storing them in an integer field in a database
table. What I''m trying to do though, using your example, is if a new record
has option 2 and option 4 selected, any other records in the table have
their option 2 and option 4 turned off. I thought VB might have a function
to do it but it looks like I''ll just have to write one that loops through
the bits and compares them individually.

Thanks again.


Hi Werden,


The And运算符和或运算符是按位的。还有Xor和

不是运营商。但是对于比按钮更大的运算符,我实际上从未听说过这样的事情。
Hi Werden,

The And operator and Or operators are bitwise. As well there''s the Xor and
Not operators. But as to a greater than operator that is bitwise, I
actually have never heard of such a thing.

如果A = 3和B = 9然后结果将是2.
So that if A = 3 and B = 9 then result would be 2.



我没有得到这个。 3将是位0011,而9将是1001,那么你将如何获得2美元?


也许A Xor(A和B)?


" werdan" < fo ******** @ that.gmail.fad.com写了留言

新闻:eF ************** @ TK2MSFTNGP05.phx .gbl ...

I don''t get this. 3 would be in bits 0011, while 9 would be 1001, so how
would you get 2 ?

Maybe A Xor (A And B) ?

"werdan" <fo********@that.gmail.fad.comwrote in message
news:eF**************@TK2MSFTNGP05.phx.gbl...


G''day all


我用bitmapping整数来存储组合布尔值和已经打了一个

有点障碍。


我希望能够关闭某些位,如果它们匹配的数字

但如果他们不(如xor的情况下)则不打开它们。基本上我是

希望使用逻辑运算A B.


例如..


AB结果

0 0 = 0

0 1 = 0

1 0 = 1

1 1 = 0

因此,如果A = 3且B = 9,则结果为2.


VB中是否存在逐位大于函数的功能?


我看着''AndAlso''功能可能会起作用但是

它只是布尔值。


非常感谢任何帮助。


干杯。


-

现实继续毁了我的生活。
G''day all

I''m bitmapping integers to store combined boolean values and have hit a
bit of a snag.

I want to be able to turn off certain bits if they they match in a number
but not turn them on if they don''t (as in the case of xor). Basically I
wish to use the logical operation A B.

For example..

A B Result
0 0 = 0
0 1 = 0
1 0 = 1
1 1 = 0

So that if A = 3 and B = 9 then result would be 2.

Is there a bit-wise greater-than function available in VB?

I was looking at the ''AndAlso'' function could possibly be made to work but
it''s boolean only.

Any help would be greatly appreciated.

Cheers.

--

Reality continues to ruin my life.


这篇关于在VB中使用整数的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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