Solved

read_bit

Posted on 2001-07-12
2
241 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now