473,586 Members | 2,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit manipulation question.

5 New Member
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.
Dec 17 '07 #1
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
Dec 17 '07 #2
Angahran
5 New Member
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
Dec 17 '07 #3
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).
Expand|Select|Wrap|Line Numbers
  1. Public Function CheckBit(ByVal intInput As Integer, _
  2.                          ByVal intBitNumber As Integer) As Boolean
  3.  
  4.     CheckBit = intInput And 2 ^ intBitNumber
  5.  
  6. End Function
  7.  
Dec 17 '07 #4
Angahran
5 New Member
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.
Dec 17 '07 #5
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
Dec 17 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. Public Function fConvertToBinary(ByVal lngValueToConvert As Long) As String
  2. 'Converts a Long Integer into its Binary equivalent and returns each Bit
  3. 'position indicating whether it is set or not
  4. Dim strBinary As String, strReturn As String, intCounter As Integer
  5. Dim intBits As Integer, intBitPosition As Integer
  6.  
  7. intBits = 24
  8. intCounter = (Len(str$(lngValueToConvert)) - 1) * 4
  9.  
  10. 'Create a String that is large enough to hold the Bits so that we can use the
  11. 'Mid Statement to replace the zeros with the real Bit Values
  12. strBinary = String(intCounter, "0")
  13.  
  14. Do While lngValueToConvert
  15.   'The remainder is the Bit Value,so set this in the String
  16.   Mid$(strBinary, intCounter, 1) = CStr(lngValueToConvert Mod 2)
  17.   lngValueToConvert = lngValueToConvert \ 2
  18.   'Decrement intCounter, so we'll know which Bit to set in strBinary
  19.   intCounter = intCounter - 1
  20. Loop
  21.  
  22. For intCounter = 1 To (intBits \ 4)
  23.   strReturn = strReturn & Left$(strBinary, 4)
  24.   strBinary = Mid$(strBinary, 5)
  25. Next intCounter
  26.  
  27. Debug.Print "*******************************"
  28. Debug.Print strReturn
  29. Debug.Print "*******************************"
  30. For intCounter = Len(strReturn) To 1 Step -1
  31.   intBitPosition = intCounter - (Len(strReturn) + 1)
  32.   Debug.Print "Bit #" & Format$(intBitPosition, "00") & " - " & _
  33.               IIf(Mid$(strReturn, intCounter, 1) = 1, "True", "False")
  34. Next
  35. End Function
Expand|Select|Wrap|Line Numbers
  1. 'Function Call
  2. Call fConvertToBinary(65535)
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. *******************************
  2. 00001111111111111111
  3. *******************************
  4. Bit #-01 - True
  5. Bit #-02 - True
  6. Bit #-03 - True
  7. Bit #-04 - True
  8. Bit #-05 - True
  9. Bit #-06 - True
  10. Bit #-07 - True
  11. Bit #-08 - True
  12. Bit #-09 - True
  13. Bit #-10 - True
  14. Bit #-11 - True
  15. Bit #-12 - True
  16. Bit #-13 - True
  17. Bit #-14 - True
  18. Bit #-15 - True
  19. Bit #-16 - True
  20. Bit #-17 - False
  21. Bit #-18 - False
  22. Bit #-19 - False
  23. Bit #-20 - False
Dec 18 '07 #7
Angahran
5 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. 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.
Mar 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2283
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 =...
4
4053
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
9
2584
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...
9
2387
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
2
4163
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*/
0
1129
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...
1
1808
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
0
2571
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...
0
2340
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...
1
1581
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...
0
7911
marktang
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...
0
7839
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...
0
8200
Oralloy
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. ...
1
7954
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...
0
8215
tracyyun
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...
1
5710
isladogs
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.