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

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.