Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax problem: Reading out bits out of a byte

Status
Not open for further replies.

waldemar

Programmer
Nov 15, 2001
245
DE
I am saving several bit-flags in a Byte field which I am using in a continous form to fill some checkboxes (Value 1 = Checkbox 1, Value 2 = Checkbox 2, Value 3 = Checkbox 1 + 2, etc.). Now I am trying to find a way to to parse this byte by bit-comparison inside the recordsource property of the checkbox. Just can't find the proper syntax - none of these work:

(for example Checkbox No.4:)

=If([intByte] && 8; True; False)
=If([intByte] & 8; True; False)
=If([intByte] And 8; True; False)

Is it possible that there is no way of comparing by Bit?
 
i dont really understand what you are doing but try this

=if([intbyte] = 8;true;false)

what is the point of tryng to use a bit when you have strings and numeric data
i think you are trying to hard make it simpler
"What a wonderfull world" - Louis armstrong
 
Thanks for your answer chrissie...
I am expected lots of data in that table so need to save as much space as possible. I have six boolean values, Access uses up 2 Bytes per Boolean that makes 12 Bytes. Using only 1 Byte would save me 11/12 space (talking of several mbyte).

'=' doesnt work because it doesnt compare bitwise.

Checkboxes No. 1 - 2 - 3 - 4 - 5 - 6
Assigned Values 1 - 2 - 4 - 8 - 16- 32

So by checking the boxes No 1 AND 4, I get the value 9;
No. 2 AND 3 AND 6 would be 38.

In regular VBA I can read the bits out with the operator 'And' (although I was expecting '&'), but I need this bit comparison inside the recourdsource of the checkbox...
 
i stikk dont see why you should use bitcomparison if you use an integer (2bytes) to store the value in like you explain it that is the way to go just save the value in an integer "What a wonderfull world" - Louis armstrong
 
That is what I am doing. I just don't need the TWO Bytes of the "Integer"; one "Byte" is enough.

How do you suggest should I read out the *different* values? Just by comparing with '=' I can only save one of the Checkboxes, but of course I need different combinations. That is what the bitcomparison is about...
 
Well, it seems to me that you are trying to analyze a number by methods used in string comparisons. A byte indeed contains 8 bits, but they are not 8 boxes where you can store randomly 1 or 0. They are actually the positions of the most significant bits in a binary representation of a number between 0 and 255:

D Binary
0 00000000 0
1 00000001 2^0
2 00000010 2^1
3 00000011 2^1 + 2^0
4 00000100 2^2
5 00000101 2^2 + 2^0
6 00000110 2^2 + 2^1
7 00000111 2^2 + 2^1 + 2^0
8 00001000 2^3
...

255 11111111 2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0

You could accomplish your goal by assigning the checkboxes such values that any combination would be unique and establish a unique relationship between the byte value and the boxes: each value would correspond to certain boxes checked.

Not sure if the effort is worth, but if it is, this should be a start point...

HTH
[pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 
No string comparisons; of course this no reeeeal bitcomparison, i am just using the decimal equivalents. Maybe this little code shows what I mean:

Function showBitsOfByte(intByte As Byte)
If (intByte And 1) Then Debug.Print "Checkbox 1"
If (intByte And 2) Then Debug.Print "Checkbox 2"
If (intByte And 4) Then Debug.Print "Checkbox 3"
If (intByte And 8) Then Debug.Print "Checkbox 4"
If (intByte And 16) Then Debug.Print "Checkbox 5"
If (intByte And 32) Then Debug.Print "Checkbox 6"
If (intByte And 64) Then Debug.Print "Checkbox 7"
If (intByte And 128) Then Debug.Print "Checkbox 8"
End Function

Function combineBitsToByte(Optional blnBit0 As Boolean = False, Optional blnBit1 As Boolean = False, Optional blnBit2 As Boolean = False, Optional blnBit3 As Boolean = False, Optional blnBit4 As Boolean = False, Optional blnBit5 As Boolean = False, Optional blnBit6 As Boolean = False, Optional blnBit7 As Boolean = False) As Byte
combineBitsToByte = 0
If blnBit0 Then combineBitsToByte = combineBitsToByte + 1
If blnBit1 Then combineBitsToByte = combineBitsToByte + 2
If blnBit2 Then combineBitsToByte = combineBitsToByte + 4
If blnBit3 Then combineBitsToByte = combineBitsToByte + 8
If blnBit4 Then combineBitsToByte = combineBitsToByte + 16
If blnBit5 Then combineBitsToByte = combineBitsToByte + 32
If blnBit6 Then combineBitsToByte = combineBitsToByte + 64
If blnBit7 Then combineBitsToByte = combineBitsToByte + 128
Debug.Print combineBitsToByte
End Function

Now I am just looking for an elegant (expression inside the recourdsource property) implementation of this in the form...
 
Ok, by implementing an own function

Function bitCompare(intByte, intBitNumber) As Boolean
If (intByte And 2 ^ intBitNumber) Then bitCompare = True Else bitCompare = False
End Function

and calling it in the recordsource of the checkbox

=bitCompare([intGradeCode];0)

this part works fine. Now I face the problem that the checkbox is not check- or uncheckable by the user. So is there is really no way around making it 6 true boolean fields (TWELVE byte!!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top