Solved

Pulling out '0' and '1' from a byte

Posted on 1998-10-18
4
149 Views
Last Modified: 2010-04-30
Is there a easy way of telling what bit's are set in a byte?
Can someone please give me an example of a function that takes a byte (0-255) as input and leaves perhaps a string containing 8 chars i.e "00101101" as a result?
I need to test if certain bit's are set, for example value 4 (the third from the right) ignoring other bit's that are set.
0
Comment
Question by:rakkas
4 Comments
 
LVL 2

Accepted Solution

by:
schild earned 100 total points
ID: 1440510
try this, its working


Private Function ByteToBool(ByVal ByteVal As Byte) As String

    Dim Index As Integer
   
    For Index = 1 To 8
        ByteToBool = IIf(ByteVal And 1, "1", "0") & ByteToBool
        ByteVal = ByteVal / 2
    Next
   
End Function


Good Luck
Schild
0
 
LVL 2

Expert Comment

by:Poddy
ID: 1440511
I would recommed using the And operator.

To test for bits being set, take the decimal value of the bit, and AND it against the tested value.

If it is set it will return the value that you are testing for, if not, it will return zero

Bracket the AND expression or you may get strange results (AND has lower priority than =)

So

(7 and 4)  = 4
(8 and 4)  = 0

etc



0
 
LVL 1

Author Comment

by:rakkas
ID: 1440512
Thanks, Poddy and schild.
You saved my day!!!
0
 

Expert Comment

by:namiesiao
ID: 5414596
The above doesnt seems to work with some numbers. Take 100 for example. I've modified it a bit into the following code. Thanks for your code though, schild.

Public Function ByteToBin(ByVal ByteVal As Byte) As String
Dim Index As Integer

For Index = 1 To 8
    ByteToBin = IIf(ByteVal Mod 2, "1", "0") & ByteToBin
    ByteVal = ByteVal \ 2
Next
End Function
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now