473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

is the byte on or off

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
12 1496
"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
Brilliant, thank you Randy.
Nov 13 '05 #3
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
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
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

"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
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 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.