?
Solved

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

Posted on 1998-10-18
4
Medium Priority
?
159 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 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

801 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