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
Solved

read_bit

Posted on 2001-07-12
2
266 Views
Last Modified: 2008-02-01
Iam have program in C++Builder and I want to read specific bit in String Can I do that and how??
exmple(A=(1000001)bin I want to know the fouth bit is 0 or the last bit is 1)
0
Comment
Question by:aminmohsalih
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 6277754
You will need to obtain the byte that the bit is stored in, then extract the bit from there with operator &.

example follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6277786
Thsi function miight be useful to you

const itn BitPerChr = 8*sizeof(char); // Might need to be changed
                          // for machines that don't store 8 bits per byte.

bool TestStringBit(const char *Str, // string to extract from.
                        int BitNum)   // 0-based bit number of bit to extract.
{
    int BytNum = BitNum / BitPerChr;
    BitNum %= BitPerChr;

   assert(BytNum < strlen(Str)); // Might want this as a permenant test.
   int Byt = Str[BytNum];  // Get desired byte.
   int Msk = 1 << (BitNum-1);  // mask for the desired bit.

   return (Byt & Msk) != 0;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 62
PDF library for Delphi 2 128
Writing a parser for java language 4 83
Outlook 13 41
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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