Link to home
Start Free TrialLog in
Avatar of aminmohsalih
aminmohsalih

asked on

read_bit

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)
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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;
}