Gemma -
SQL's "AND" operator is logical and there's no bitwise operator in SQL. It may work in VBA since VBA's "And" operator is bitwise and by extension could be also used as logical operation with caveats.
In SQL, though a "AND" would only give you "True" (aka -1) for the expression "128 AND 64" which isn't equal to 64 and therefore is false.
Furthermore, "8 AND 4" = -1 but do it in VBA and "8 And 4" = 0. Both are correct as long one is mindful of whether the And operator is a logical or bitwise operator and it's being used accordingly.
So the whole point of this thread was to try and nail down the mathematical equivalent to the bitwise expression.
I am not quite following this - why would 8 AND 4 return true in a SQL AND. Surely 8 AND 4 is still false.
anyway, I researched further,and came across a reference to & as a bitwise operator in SQL
http://www.mssqltips.com/tip.asp?tip=1218
And was there some constraint on calling a function from the query?
I do understand base systems. But the reasoning for this thread is still legit. =) It may require further testing on my part to get to a mathematical equivalent to NOT bitwise in a query, but lagbolt's expression may just give me a hint!Emulating fully fledged bitwise operations with Base10 arithmetic functions is certiainly not going to be performed with any manageable expression.
Essentially, arithmetic operations in SQL are functions designed to work with Base10 arguments. Base2 is completely foreign to them so any attempt to use arithmetic operators on binary must first deal with a base translation. Hence 2^n is evident in every expression we have discussed.
Moreover, bitwise is really just a way to represent multiple independent values in a single number. The whole value does not have a meaning as such. Likewise, bitwise operations are fully independent processes on each bit pair. Consequently there is no equivalent concept in any form of arithmetic that deals with the number as a whole.
The nearest decimal equivalent would be to use each digit to represent an independent value.
Because SQL's AND operator is a logical operator, not a bitwise operator. The equivalent in VBA would be more like: "CBool(x) And CBool". When you evaluate 8 and 4 both as a Boolean values, they turn into a True value and therefore it's "True And True", giving you a True.
Galaxiom is correct but just in case - whenever I spoke of "SQL", I didn't mean SQL Server and its dialect, Transact-SQL, but rather standard ANSI SQL, which is applicable to all SQL database products (at least as far as where they follow the standard).
in that case, i presume you would have to evaluate every row in the table to achieve this anyway - so is there any real benefit trying to do this all within SQL, compared with writing a VBA function to test the bit pattern - in which case you can use bitwise operations?
I love learning...In relational databases, a condition (or predicate) in a query is said to be sargable if the DBMS engine can take advantage of an index to speed up the execution of the query (using index seeks, not covering indexes). The term is derived from a contraction of Search ARGument Able.
- From Wikipedia, the free encyclopedia
Does this mean that I can't count on any more help from you, SARGE?Sar'-ga-ble
I love learning...
Huh?Does this mean that I can't count on any more help from you, SARGE?![]()
Public Declare Function timeGetTime Lib "Winmm.dll" () As Long
Public Sub Test()
Dim lngI As Long
Dim lngStart As Long
Dim lngLoop As Long
Dim X As Long
Const conLoopCount As Long = 1000000
X = 129
lngStart = timeGetTime()
For lngI = 1 To conLoopCount
[color=green]' Time the loop by itself.[/color]
Next lngI
lngLoop = timeGetTime() - lngStart [color=green]'<< 15 milliseconds total.[/color]
lngStart = timeGetTime()
For lngI = 1 To conLoopCount
[color=green]' Time the loop with the Function call.[/color]
TestBitN X, 7
Next lngI
[color=green]' Function Call time for 1,000,000 Calls.[/color]
MsgBox timeGetTime() - lngStart - lngLoop [color=green]'<< 265 milliseconds total.[/color]
End Sub
Public Function TestBitN(ByVal lngValue As Long, _
ByVal bytBit As Byte) As Integer
[color=green]' Valid Bit numbers are 0 to 31.[/color]
If bytBit > 31 Then
TestBitN = False
Else
If bytBit = 31 Then
If lngValue < 0 Then TestBitN = True
Else
TestBitN = CBool(lngValue And (2 ^ bytBit))
End If
End If
End Function