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 4875 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_Cl ick
bit1.Value = classes.Value & 1
Exit_Command6_C lick:
Exit Sub
Err_Command6_Cl ick:
MsgBox Err.Description
Resume Exit_Command6_C lick
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(ByV al 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 identical subtrees, replace them in
the AST with a dummy variable to which I will assign the common subexpression.
v21c =...
|
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?
examples
|
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 ways
to realize, like using cursor manipulation to backup & delete
letters or otherwise to use getch to read them quetly.
Maybe somebody here knows...
|
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';
$sentence = "$insert or not $insert. That is the question.";
or
|
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 code*/
| |
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 readonly grid, still if click on a cell it turns blue. How to
prevent this ? Eventually it may be a rwo selection.
3. While 2 I still want to see...
|
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 connecter
sur le site, on verifie qu'il a les droits d'apres son login).
2) Manipulation HTTP
|
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 Manipulation Profiling Tools Set is a set of
advanced tools that help Serious Traders analyze the market direction,
market manipulative behavior and...
|
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
new TradeStation 8 Add-on - L'eau Prosper Market Manipulation
Profiling Tools Set.
L'eau Prosper Market Manipulation Profiling Tools Set is a...
|
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 company that uses a program that stores data in an SQL database running off a Firebird engine
The program itself doesnt come with database...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |