By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,036 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,036 IT Pros & Developers. It's quick & easy.

is the byte on or off

P: n/a
Dear Gurus

I have a question that is really more about maths than Access. However, I
am using it in access and may be there is an access specific command that I
may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you input a
number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true,
F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas


Nov 13 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:CC*******************@news.xtra.co.nz...
Dear Gurus

I have a question that is really more about maths than Access. However, I
am using it in access and may be there is an access specific command that I may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you input a
number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true,
F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas


Nicolaas,

A couple of small points. We usually call the individual 1's and 0' bits
rather than bytes. So:

000 ... no bits are on
128 ... here we would say that bit 7 is on (starting from 0, the 8th bit -
also defined as 2^7 )
256 ... is not all bits on - 255 is

Further:
F(256, 4) should not return true
F(255, 4) would return true

The VB bitwise operator will do exactly what you need:

Private Function F(J As Integer, K As Integer) As Boolean
F = J And K
End Function

Debug.Print F(128, 8)
True

Regards,
Randy
Nov 13 '05 #2

P: n/a
Brilliant, thank you Randy.
Nov 13 '05 #3

P: n/a
Dear Randy

I tried your formula, but it does not seem to work. Is there no
mathematical formula that I can use?

Thank you

- Nicolaas
PS in the function below B should be a number between 1 and 8, pointing to
bit 1 to 8, so I amended it as follows:
Function F(A As Integer, B As Integer) As Boolean
B = B -1
F = A And B
End Function
Nov 13 '05 #4

P: n/a
Dear Randy et al.

I also need to use the formula in SQL, therefore, a mathematical formula
would be best I think.

Someone gave me the solution
F = (A And 2 ^ (B- 1)) <> 0

which seems to work, but not in SQL.

Thank you.

- Nicolaas
Nov 13 '05 #5

P: n/a
If this data is coming from fields, be sure to explicitly typecast whatever
is passed in:

Function BitwiseAndByte(vByte1 As Variant, vByte2 As Variant) As Boolean
On Error Resume Next
BitwiseAndByte = CByte(vByte1) And CByte(vByte2)
On Error GoTo 0
End Function
In JET 4 (Access 2000 and later), you can use the BAND operator to perform a
Binary And operation, if you run your query under ADO (not DAO or the
interface, IIRC).

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"WindAndWaves" <ac****@ngaru.com> wrote in message
news:Xw*******************@news.xtra.co.nz...
Dear Randy et al.

I also need to use the formula in SQL, therefore, a mathematical formula
would be best I think.

Someone gave me the solution
F = (A And 2 ^ (B- 1)) <> 0

which seems to work, but not in SQL.

Nov 13 '05 #6

P: n/a

"WindAndWaves" <ac****@ngaru.com> wrote in message
news:Xw*******************@news.xtra.co.nz...
Dear Randy et al.

I also need to use the formula in SQL, therefore, a mathematical formula
would be best I think.

Someone gave me the solution
F = (A And 2 ^ (B- 1)) <> 0

which seems to work, but not in SQL.

Thank you.

- Nicolaas


I think I misinterpreted what you are trying to accomplish. It looks as
though, rather than passing two "values" to the function, you wish to pass a
single value and a "bit number". The "bit number" to be in the range of 1 -
8 with 1 being the lowest order bit. If all of that is correct, then the
forumula that you have above will work.

That formula wrapped into a simple function (using Allen's suggestion to
force typecasting):

Public Function BitCheck(varValue as Variant, varBit as Variant) As Boolean
On Error Resume Next
BitCheck = CInt(varValue) And (2 ^ (CInt(varBit) - 1))
ON Error Goto 0
End Function

HTH,
Randy
Nov 13 '05 #7

P: n/a
In addition to the answers you have already received, please note that a
standard definition of a Byte encompasses values from 0 to 255 for a
total of 256 individual values.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:CC*******************@news.xtra.co.nz...
Dear Gurus

I have a question that is really more about maths than Access. However, I am using it in access and may be there is an access specific command that I may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you input a number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true,
F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas


Nov 13 '05 #8

P: n/a
Dear Allen

Thank you for your answer.

Band is out of the question I think, because I am using DAO.

Here is the function as I have it right now, and it seems to work:

Public Function FIDRLU(IDR As Byte, N As Byte) As Boolean
'IDR (0-255): returns a true or false value for a particular IDR
'N (1-8): for a chosen item

FIDRLU = (IDR And 2 ^ (N - 1)) <> 0

End Function

However, I was hoping to also find a code snippet that I could put into SQL
and that does not use a module (to speed things up).

The IDR values that I put in are always bytes, because they come from a byte
field. The N values that you passed, are from other modules and I will make
sure that they are always between 1 and 8. I could even put in a line like
if N<1 or N >8 then FIDRLU = false: exit function,

but so far I have left this out, to keep up the speed.

Thank you all once more for your help.
Nov 13 '05 #9

P: n/a
Dear Everyone,

Thank you for all your answers.

I have solved the problems I think by using the following syntax to create
SQL from VB:

Public Function RSSQL(TblN As String, n As Byte, B As Boolean) As String
'provides the SQL for a particular table,
'for a particular record status
'for a particular status (true or false)
'e.g. select all the the contacts who are archived
Const ProEro = 1 ': 'on error GoTo err
'---
n = n - 1
RSSQL = "SELECT [" & TblN & "].* FROM [" & TblN & "] WHERE
((((Int([IDR]/2^" & n & ") Mod 2)=1)=" & B & "));"
xit:
Exit Function
ERR:
RSSQL = "SELECT * FROM [" & TblN & "];"
Call FerrorLog(ERR.Number, 0, ProEro + ModEro): Resume Next
End Function
For a straight VB function, I use the following syntax.
Public Function FIDRLU(IDR As Byte, n As Byte) As Boolean
'IDR (0-255): returns a true or false value for a particular IDR
'N (1-8): for a chosen flag
'e.g. fidrlu can tell you whether the N(2) flag = default record is on for
record
' with IDR 128, the answer is false in that case, but if IDR was 2 then
'the answer was true
Const ProEro = 1: 'on error GoTo ERR
'---
FIDRLU = (IDR And 2 ^ (n - 1)) <> 0
xit:
Exit Function
ERR:
Call FerrorLog(ERR.Number, 0, ProEro + ModEro): Resume xit
End Function

Thank you all once more for your help. Much appreciated.
Nov 13 '05 #10

P: n/a
Essentially you test t see whether the number anded with a number which is
equivalent to the bit position number is equal to the number anded with the
bit position.

so because
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

and so on to
2^8 = 256

To test if a particular bit is on you can do this

Function TestBit(NumTest As byte, BitPosn As Integer) As Boolean
TestBit = ((NumTest And (2 ^ BitPosn)) = (2 ^ BitPosn))
End Function

--
Terry Kreft
MVP Microsoft Access
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:CC*******************@news.xtra.co.nz...
Dear Gurus

I have a question that is really more about maths than Access. However, I
am using it in access and may be there is an access specific command that I may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you input a
number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true,
F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas

Nov 13 '05 #11

P: n/a
Hi Terry,
nice function!
Since we are speaking of Byte values, the highest bit position would be
2^7 = 128
:-)
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:o2********************@karoo.co.uk...
Essentially you test t see whether the number anded with a number which is equivalent to the bit position number is equal to the number anded with the bit position.

so because
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

and so on to
2^8 = 256

To test if a particular bit is on you can do this

Function TestBit(NumTest As byte, BitPosn As Integer) As Boolean
TestBit = ((NumTest And (2 ^ BitPosn)) = (2 ^ BitPosn))
End Function

--
Terry Kreft
MVP Microsoft Access
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:CC*******************@news.xtra.co.nz...
Dear Gurus

I have a question that is really more about maths than Access. However, I am using it in access and may be there is an access specific command that
I
may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you

input a number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true, F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas



Nov 13 '05 #12

P: n/a
Stephen,
Erm, you could be right (red-faced look).
(Terry repeats constantly
8 bits to the byte, 8 bits to the byte
0 to 7, 0 to 7 will fill it up just right)

--
Terry Kreft
MVP Microsoft Access
"Stephen Lebans" <Fo****************************************@linval id.com>
wrote in message news:rr*********************@ursa-nb00s0.nbnet.nb.ca...
Hi Terry,
nice function!
Since we are speaking of Byte values, the highest bit position would be
2^7 = 128
:-)
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:o2********************@karoo.co.uk...
Essentially you test t see whether the number anded with a number

which is
equivalent to the bit position number is equal to the number anded

with the
bit position.

so because
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

and so on to
2^8 = 256

To test if a particular bit is on you can do this

Function TestBit(NumTest As byte, BitPosn As Integer) As Boolean
TestBit = ((NumTest And (2 ^ BitPosn)) = (2 ^ BitPosn))
End Function

--
Terry Kreft
MVP Microsoft Access
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:CC*******************@news.xtra.co.nz...
Dear Gurus

I have a question that is really more about maths than Access. However, I am using it in access and may be there is an access specific command that
I
may use to achieve what i need (as outlined below).

Here is my question. I need a formulae that tells me for any number
between 0 and 256 what bytes are "on"

for example

000 ... no bytes are on
256 ... all bytes are on
128 ... only byte 8 is engaged

I am writing a function in VB that returns true or false after you

input a number and a byte (e.g. F(128, 8) returns true, F(129,1) returns true, F(130, 1) returns false, F(256, 4) returns true, etc...).

Thank you

- Nicolaas


Nov 13 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.