Solved

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

Posted on 1998-10-18
4
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

717 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