How to read a binary file bit by bit?

I want to read a binary file bit by bit directly but not to Vec<u8>?

Check out bitvec

1 Like

To be specific, you would need to read the file into a Vec<u8> initially. Subsequently, you can make a BitSlice from it and read it bit-wise.
I have a better answer here.

I've tried the crate, but I don't find an api to read to BitSlice directly.

Couldn't one instead wrap std::io::Bytes<R> to yield bits rather than bytes, and thus Read a file bit-by-bit?

Actually, I just noticed that BitVec implements Write, which means you can do the following with a given File "object":

let mut vec = BitVec::new();
io::copy(&mut file, &mut vec);

Now you can read bitwise from vec.

2 Likes

There's also BitReader — Rust implementation // Lib.rs

2 Likes