0

I have seen familiar question, but nothing that helped me solve my problem.

Say I have a binary file. I want to collect every 3 written bits (in our case, filesize%3 == 0) and check wether the 3rd bit is the result of the " | " operation for the first two bits. For example, if this is our binary file: 111000101

So I first I want to get 111, and then check if 1|1 is 1. Next, I want to get 000, and then check if 0|0 = 0. Last, I want to get 010, and then check if 0|1 = 1.

Now, what I have been thinking of, is to create unsigned char arr[3] that holds {1,1,1} and then send the array to a function that will check if 1|1 = 1.

The problem is, that I can't think of a way to get specific bits from a given file.

Does anyone have any idea how I can get this information?

Thanks in advance!

4
  • 3
    Are you want to deal with a binary file (a bit is expressed as a bit) or a text file where characters 0 and 1 are used to express bits? Commented Jun 23, 2021 at 16:36
  • 2
    Make a little function that will serve up sets of 3 bits, from a buffer, that has static variables to keep track of the bit position etc. If the buffer does not have enough bits, replenish and realign from the file buffer, and if that has no bytes, read some more (or just one) from the file. Commented Jun 23, 2021 at 16:37
  • 1
    Continuing on @MikeCAT comment, can you tell us how many bits has a file created by opening your text editor (Notepad or vim), writing abc and then saving? Commented Jun 23, 2021 at 17:40
  • 1
    If it is at bit level : 8 packs of 3 bits give 24 bits which fits exactly in 3 bytes. So if you read your file three bytes by three bytes, you can quickly extract "third" bits with an AND 0x249249 and the "two significant" bits with an AND 0xDB6DB6. After, it 's boolean maths/ops to verify both mask. Commented Jun 23, 2021 at 18:14

1 Answer 1

1

C file I/O (and C in general) is organized around bytes1 not bits. So there's generally no way to access specific bits in a file, just bytes.

So if you want to manipulate bits within a file, you need to read the bytes that contain the bits you are interested in and then extract the bits (generally using shifts and masks). If you want to write those bits back, you'll need to repack them into entire bytes.

One way is to have a bit stream abstraction layered on top of a C FILE * that can extract the bits from each byte read (keeping the remaining bits for a later read). Something like:

#include <stdio.h>
#include <limits.h>

struct bit_stream {
    FILE *fp;   // source to read from
    int byte;   // last byte read from fp
    size_t bits;   // how many bits are left in byte;
};

int read_bits(struct bit_stream *bs, size_t size) {
    if (size >= sizeof(int)*CHAR_BIT) {
        fprintf("size %z is too big\n", size);
        exit(1); }
    int rv = 0;
    size_t bits = 0;
    while (bits < size) {
        if (!bs->bits) {
            if ((bs->byte = fgetc(bs->fp)) == EOF)
                return bits ? rv : EOF;
            bs->bits = CHAR_BIT; }
        if (bs->bits > size - bits) {
            size_t extract = size - bits;
            rv |= (bs->byte & ((1U << extract) - 1)) << bits;
            bs->bits -= extract;
            bs->byte >>= extract;
            bits += extract;
        } else {
            rv |= bs->byte << bits;
            bits += bs->bits;
            bs->bits = 0; }
    }
    return rv;
}

int main() {
    struct bit_stream bs = { stdin, 0, 0 };
    int v;
    while ((v = read_bits(&bs, 3) != EOF) {
        printf("%d\n", v);
    }
}

This uses read_bits(&bs, 3) to read the next 3 bits from the file.


1Bytes are pretty much always 8 bits these days, but the C standard still allows for other sizes of bytes, as specified by CHAR_BIT from <limits.h>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.