Re: Usisng logical operators with integers in VB
- From: "werdan" <footrotdog@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 29 Apr 2008 15:53:59 +1000
"Bill McCarthy" <Bill@xxxxxxxxxx> wrote in message news:OJEjoQbqIHA.524@xxxxxxxxxxxxxxxxxxxxxxx
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) ?
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
.
- Follow-Ups:
- Re: Usisng logical operators with integers in VB
- From: Steve Gerrard
- Re: Usisng logical operators with integers in VB
- From: Bill McCarthy
- Re: Usisng logical operators with integers in VB
- References:
- Usisng logical operators with integers in VB
- From: werdan
- Re: Usisng logical operators with integers in VB
- From: Bill McCarthy
- Usisng logical operators with integers in VB
- Prev by Date: Re: ToolStripButton does not fire click event if form does not have focus?
- Next by Date: Re: Usisng logical operators with integers in VB
- Previous by thread: Re: Usisng logical operators with integers in VB
- Next by thread: Re: Usisng logical operators with integers in VB
- Index(es):
Relevant Pages
|