?
Solved

read_bit

Posted on 2001-07-12
2
Medium Priority
?
282 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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