Usisng logical operators with integers in VB

W

werdan

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.
 
T

Tom Shelton

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.

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
 
W

werdan

Tom Shelton said:
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


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.
 
B

Bill McCarthy

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.
So that if A = 3 and B = 9 then result would be 2.

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) ?
 
W

werdan

Bill McCarthy said:
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.


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) ?



okay. Using Tom's options enum

Option1 = &H1
Option2 = &H2
Option3 = &H4
Option4 = &H8

Say I had Option1 (&H1) and Option2 (&H2) set to true for a record (A) in a
table, the integer would be stored as 3. Now I add a new record (B) and it
has Option1 and Option4 (&H8) set to true so it's stored as 9. *But*
although I want to store multiple settings, I don't want have duplicate
settings across these records, so I need to remove the Option 1 from the
record A.

A B = New A Value
0 1 = 0
0 0 = 0
1 0 = 1
1 1 = 0

So that A now equals 0010 or 2, B is still 1001 or 9 and now don't have
any duplicate options set.


I ended up just making a function to do it like this..

Shared Function logical_A_GreaterThan_B(ByVal A As Integer, ByVal B As
Integer) As Integer

Dim i As Integer
Dim n As Integer

For i = 0 To 30 ' 2^31 overflows an integer by 1 so let's just
do the first 30 bits

n = CInt(2 ^ i)

If CBool(A And n) Then 'if bit in A is 'on'
If CBool(B And n) Then 'if bit in B is 'on'
A -= n 'Turn bit in A
'off'
End If
End If

Next

Return A

End Function
 
W

werdan

Steve Gerrard said:
I think you just need
A And (Not B)

(Not B) is the inverse of B, so if B was 1001, (Not B) would be 0110. If
you now And that with A, it will allow A to retain its 1's only where B
was originally 0.

Yes. I'm having one of those days.

Thanks all for your replies.
 
C

Cor Ligthert [MVP]

Werdan,

While I saw at least 2 guys busy from who I have the idea they dream in
Boolean expressions, I did not even try.

However this should do it in my idea.

Dim C As Integer = IIf(A - B < 0, 0, A - B)
Cor
 
R

raylu

My question is how to overloading Operator ++ in VB.net. And it works in C#
but in vb.net give me error and not think it is operator.
 
A

Andrew Morton

raylu said:
My question is how to overloading Operator ++ in VB.net. And it works
in C# but in vb.net give me error and not think it is operator.

There is no ++ operator in VB.NET.

Andrew
 
B

Bill McCarthy

Hi Steve, Werden,

Yeh I should have seen that too. I had A XOr (A And B), and then when I
heard the problem a bit more clearly I thought that's A And Not (A And B)
and knew that coudl be reduced but it wasn't coming to me ;)

Yes. I'm having one of those days.

Thanks all for your replies.


I put together a post on bitwise algebra on my blog that shows some rules
for manipulating of them.
http://msmvps.com/blogs/bill/archive/2008/04/30/bit-wise-algebra.aspx
 
C

Cor Ligthert[MVP]

Joergen,

I saw this but basicly he wrote first
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

I find his question very unclear, as you create your conclusion only on this
part of his message.
So that if A = 3 and B = 9 then result would be 2.
Is there a bit-wise greater-than function available in VB?

Then you should in my idea learn to handle a problem complete, and not base
it on what you think that the problem can be.

His message is only about logical operators nowhere about Boolean
operators.. What you add is only your personal contribution and guessing
that it is the problem (However I think that it is right or not far beside
it. But it is not the answeron the quesion of the OP).

Don't think that I had the idea my answer was the solution, it was only to
answer on the above problem to get a better explanation.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top