Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read_bit

Posted on 2001-07-12
2
Medium Priority
?
294 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
[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
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 120 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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 will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

597 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