Dear all.
please kindly but freely provide of your ideas and thought about following.
I need to get binary bits of variety of files regardless of ASCII, Unicode, or even non-printable etc. I searched following seems to be commonly used but not for all.

bits[i] = ((1<<i) & c) != 0 ? 1:0;

bitset<8> qBits(buffer[j]);

Will you please provide any other way of getting bits of files???
This is important task for me, and any of your response will highly be appreciated.
Thanks in advance for your attention.
Sincerely
Ted

Dani AI

Generated

Short summary: treat every file as a stream of raw bytes (open in binary), read into an unsigned byte buffer, and decide explicitly whether bits should be MSB-first or LSB-first. Encoding (ASCII/UTF-8/UTF-16) only matters if the goal is to inspect logical characters; otherwise operate on raw bytes.

Notes on the existing replies: ’s approach demonstrates the basic ideas, but a few practical issues need calling out — don’t use feof() as the loop condition, read getc() into an int so EOF is detectable, avoid writing past array bounds (the null terminator index was off-by-one), and never call printf with raw data as the format string (use printf("%s", s) or fputs). Also beware of signed char when shifting: cast to unsigned char or use uint8_t to avoid sign-extension.

A fast, robust pattern for large files is a 256-entry lookup table: precompute the 8-bit representation for each possible byte, then read the file in large blocks (fread/ifstream::read) and map each buffer byte through the table. This removes the inner bit loop and gives good throughput without exotic APIs. Example (C++11-style):

// build MSB-first string table
std::array<std::string,256> table;
for (int b=0;b<256;++b) {
  std::string s; s.reserve(8);
  for (int i=7;i>=0;--i) s.push_back(((b>>i)&1) ? '1' : '0');
  table[b]=s;
}
// buffered read + lookup
std::ifstream in("file.bin", std::ios::binary);
char buf[1<<16];
std::string out; 
while (in.read(buf,sizeof(buf)) || in.gcount()) {
  std::streamsize n=in.gcount();
  for (std::streamsize j=0;j<n;++j) out += table[static_cast<unsigned char>(buf[j])];
}

Other options: memory-map very large files for zero-copy access; use std::bitset for clarity if performance isn’t critical; avoid vector<bool> for storage semantics. If the goal is bits of logical text (code points), decode the encoding first, then extract bits from the code points.

Recommended Answers

All 2 Replies

If you are trying to break a byte into bits you can do this:

struct byte{
    bool bit[8];
};
byte breakByte(uint8_t val)//break val into individual bits
{
    byte ret;
    for (int i=0; i<8; ++i)
        ret.bit[i]=((val>>i)&1);
    return ret;
}
byte fastBreakByte(uint8_t val)//this uses loop unwinding
{
    //by negating the loop, this may be faster depending on how well your compiler optimizes
    byte ret;
    ret.bit[0]=val&1;
    ret.bit[1]=(val>>1)&2;
    //...
    return ret;
}
bool getBit(uint8_t val, int ind)//get a specific bit
{
    return (val>>ind)&1;
}

Is this what you are asking for?

Here is a program I wrote for in october. It can even be used as a quine (command-line freadbits.exe freadbits.exe).

#include <stdio.h>
int main(int argc, char *argv[])
{
    if (argc!=2)
    {
        printf("freadbits [filename].[file-extension]\n\tShows the content of the given file.");
        return 1;
    }
    FILE *file=fopen(argv[1],"rb");
    if (!file)
    {
        char *fname;
        sprintf(fname,".\\%s",argv[1]);
        file=fopen(fname,"rb");
        if (!file)
        {
            printf("freadbits [filename].[file-extension]\n\tShows the content of the given file.");
            return 1;
        }
    }
    char tmp;
    while (!feof(file))//loop through the whole file
    {
        tmp=getc(file);
        char out[9];
        for (int i=0; i<8; i++)//create the output string
            ((tmp&(1<<i))>0)?out[i]='1':out[i]='0';
        out[9]=0;//null terminate the string
        printf(out);
    }
    return 0;
}

This should be the answer to your question (granted it is more C than C++).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.