Hi,
I've been trying to do something that seems like ist should be simple but I'm not having any luck.
I have an integer field with values from 0 to 65535 which is 16 flags.
What I want to do is test for each of these flags individually and display if they are true or false.
However, when I try ANDing the field with 1, 2, 4, etc to get the bits, it always returns -1 (unless the field is 0 then it returns 0).
Thought of manually making a lookup table but the thought of typing 65535 rows of yes/no made my hands start to ache.
Sounds simple, but I'm pulling my hair out trying to get it to work.
Any suggestions much appreciated.
7 4852 FishVal 2,653
Recognized Expert Specialist
Hi, Angahran.
It looks like the operation is performed as boolean (True=-1, False=0). I couldn't reproduce your situation. I get bitwise operators AND, OR, XOR working as expected. Could you post an exact code you have?
Regards,
Fish
Here's the dummy code I've been trying out.
classes is the field in the table and bit1 is my output field.
Eventually I'd like to be able to check all 16 bits, but getting 1 to work would be a good start :P
Private Sub Command6_Click()
On Error GoTo Err_Command6_Click
bit1.Value = classes.Value & 1
Exit_Command6_Click:
Exit Sub
Err_Command6_Click:
MsgBox Err.Description
Resume Exit_Command6_Click
End Sub
FishVal 2,653
Recognized Expert Specialist
:)
VBA syntax differs form C one.
"&" in VBA is "AND"
"|" in VBA is "OR"
"^" in VBA is "XOR"
The following simple function checks specified bit in Integer(word). -
Public Function CheckBit(ByVal intInput As Integer, _
-
ByVal intBitNumber As Integer) As Boolean
-
-
CheckBit = intInput And 2 ^ intBitNumber
-
-
End Function
-
Thanks :)
Was getting overflow on anything above 32767 so changed to Double.
Public Function DblCheckBit(ByVal dblInput As Double, _
ByVal dblBitNumber As Double) As Boolean
DblCheckBit = dblInput And 2 ^ dblBitNumber
End Function
Now going to dummy up a form with 'Classes' and 16 True/Falses and see how that looks.
Thanks again.
FishVal 2,653
Recognized Expert Specialist
Better change it to Long, though storing 16 bit values you don't need it as soon as Integer values above 32767 are represented as negative (VBA doesn't have unsighned types).
Regards,
Fish
ADezii 8,834
Recognized Expert Expert
Hi,
I've been trying to do something that seems like ist should be simple but I'm not having any luck.
I have an integer field with values from 0 to 65535 which is 16 flags.
What I want to do is test for each of these flags individually and display if they are true or false.
However, when I try ANDing the field with 1, 2, 4, etc to get the bits, it always returns -1 (unless the field is 0 then it returns 0).
Thought of manually making a lookup table but the thought of typing 65535 rows of yes/no made my hands start to ache.
Sounds simple, but I'm pulling my hair out trying to get it to work.
Any suggestions much appreciated.
I have an integer field with values from 0 to 65535 which is 16 flags
The maximum allowable value for an Integer is 32767 (a Bit is assigned to indicate whether or not the Number is Positive or Negative).
Here is a little something I threw together for you. Take a look at it and see if it is what you need. Simply pass to the Function a Long Integer and let it do the work for you: - Public Function fConvertToBinary(ByVal lngValueToConvert As Long) As String
-
'Converts a Long Integer into its Binary equivalent and returns each Bit
-
'position indicating whether it is set or not
-
Dim strBinary As String, strReturn As String, intCounter As Integer
-
Dim intBits As Integer, intBitPosition As Integer
-
-
intBits = 24
-
intCounter = (Len(str$(lngValueToConvert)) - 1) * 4
-
-
'Create a String that is large enough to hold the Bits so that we can use the
-
'Mid Statement to replace the zeros with the real Bit Values
-
strBinary = String(intCounter, "0")
-
-
Do While lngValueToConvert
-
'The remainder is the Bit Value,so set this in the String
-
Mid$(strBinary, intCounter, 1) = CStr(lngValueToConvert Mod 2)
-
lngValueToConvert = lngValueToConvert \ 2
-
'Decrement intCounter, so we'll know which Bit to set in strBinary
-
intCounter = intCounter - 1
-
Loop
-
-
For intCounter = 1 To (intBits \ 4)
-
strReturn = strReturn & Left$(strBinary, 4)
-
strBinary = Mid$(strBinary, 5)
-
Next intCounter
-
-
Debug.Print "*******************************"
-
Debug.Print strReturn
-
Debug.Print "*******************************"
-
For intCounter = Len(strReturn) To 1 Step -1
-
intBitPosition = intCounter - (Len(strReturn) + 1)
-
Debug.Print "Bit #" & Format$(intBitPosition, "00") & " - " & _
-
IIf(Mid$(strReturn, intCounter, 1) = 1, "True", "False")
-
Next
-
End Function
- 'Function Call
-
Call fConvertToBinary(65535)
OUTPUT: -
*******************************
-
00001111111111111111
-
*******************************
-
Bit #-01 - True
-
Bit #-02 - True
-
Bit #-03 - True
-
Bit #-04 - True
-
Bit #-05 - True
-
Bit #-06 - True
-
Bit #-07 - True
-
Bit #-08 - True
-
Bit #-09 - True
-
Bit #-10 - True
-
Bit #-11 - True
-
Bit #-12 - True
-
Bit #-13 - True
-
Bit #-14 - True
-
Bit #-15 - True
-
Bit #-16 - True
-
Bit #-17 - False
-
Bit #-18 - False
-
Bit #-19 - False
-
Bit #-20 - False
Thanks for all the help.
Slowly beating this form into shape, but seems like there's a new problem hiding behind every line of code.
Apparently, I have hit some maximum number of fields limit on my form when I tried linking the lookup tables I had created.
So, I thought I'd try working without the lookup tables and check the bits as part of the main query.
Here's a dummy part showing what I was trying to do. - SELECT classes FROM qryItems WHERE (CheckBit(classes,0) = 'True');
Unfortunately, no matter how I specify the 'True' part I get a type mismatch error and Access '07 crashes :(
I've tried single quotes, double quotes, no quotes, all with the same type mismatch error.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Robert Kern |
last post by:
I'm in the middle of writing a scientific program with a number of very, very
long formulae. You can see a typical one below. What I would like to do is to
parse this code to yield an AST, identify...
|
by: Elijah Bailey |
last post by:
I have a long x;
I want to write a function
long f(long x, int k)
such that it extracts every k-th bit of x, concatenates
them and returns it. Anyone can help me in writing this
function?
...
|
by: I. Kobrinsky |
last post by:
I'm new here.
I started a personal password-program, a trial that includes
username, logincounter and password. So my intention is to
hide pwd while tipping. So I'm thinking about two popular...
|
by: Rune |
last post by:
Is it best to use double quotes and let PHP expand variables inside strings,
or is it faster to do the string manipulation yourself manually?
Which is quicker?
1)
$insert = 'To Be';...
|
by: Arun Prasath |
last post by:
Hi all,
I have the following question regd pointer typecasting. Is the
following type of pointer typecasting valid?
#define ALLOC(type,num) ((type *)malloc(sizeof(type)*num))
/*begin...
| |
by: Wilfried Mestdagh |
last post by:
Hi,
I have a couple of dataGridView manipulation question:
1. The datetime is not displayed in the way I have set it in my system, how
to set it the way my system is configured ?
2. I have...
|
by: bob |
last post by:
1) Authentification NT:
En ASP (pas ASP.Net),
comment on recupere le login/pwd windows ?
On veut automatiquement authentifier un utilisateur d'après son login NT (il
a pas besoin de se...
|
by: L'eau Prosper Research |
last post by:
Press Release:
L'eau Prosper Research (Website: http://www.leauprosper.com) releases
new TradeStation 8 Add-on - L'eau Prosper Market Manipulation
Profiling Tools Set.
L'eau Prosper Market...
|
by: L'eau Prosper Research |
last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation
Profiling Tools Set By L'eau Prosper Research
Press Release:
L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
|
by: Rhadamanthys |
last post by:
Hello All
I am a relative beginner to SQL databases & new to this forum, so please bear with me if my query is too basic and advise if this question belongs somewhere else
I began working at a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |