How to programmatically determine whether a given binary/decimal number contains a specific binary place value

Posted on 2005-03-15
Medium Priority
Last Modified: 2010-04-23
I'm trying to find the most efficicent method for evaluating a given number and determining which binary place values it is made up of.  For example:

168 = 1010 1000 (is made up of the '128', '32' and '8' bits)

My goal is to be able to return a boolean value for each place value, indicating whether it is contained within the given number.  I'm hoping there may be a "built-in" method for doing this simpler than coding the math manually!  Does anyone have any ideas?  Thanks!
Question by:esdTeamsters
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
  • 3
  • 2

Expert Comment

ID: 13547829

this link should prove helpful, it converts decimal to binary and vice versa:

LVL 42

Expert Comment

ID: 13547832
Use the "AND" logical operator.  For example:

If 168 AND 128 = 128 then
  '128 bit is set
  '128 bit is not set
End If
LVL 42

Accepted Solution

frodoman earned 2000 total points
ID: 13547849
A more generic form of my last posting:

function IsBitSet(TestValue as Integer, BitValue as Integer) as Boolean
  Return ((TestValue AND BitValue)=BitValue)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13547909
Thanks for the quick response!  I'm testing it out right now...

Author Comment

ID: 13548074
Outstanding!  works great, frodoman.  Wim_Bl, thanks for the input, but the article wasn't specific to what I was looking for - I'm already converting binary to decimal and vice versa.

Thanks again all!
LVL 42

Expert Comment

ID: 13548135
Glad I could help.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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