From: Alfred Bovin on
Hi all.

I'm working on something where I need to read a (binary) file bit by bit and
do something depending on whether the bit is 0 or 1.

Any help on doing the actual file reading is appreciated.

Thanks in advance


From: Ulrich Eckhardt on
Alfred Bovin wrote:
> I'm working on something where I need to read a (binary) file bit by bit
> and do something depending on whether the bit is 0 or 1.

Well, smallest unit you can read is an octet/byte. You then check the
individual digits of the byte using binary masks.


f = open(...)
data = f.read()
for byte in data:
for i in range(8):
bit = 2**i & byte
...

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: Peter Otten on
Alfred Bovin wrote:

> I'm working on something where I need to read a (binary) file bit by bit
> and do something depending on whether the bit is 0 or 1.
>
> Any help on doing the actual file reading is appreciated.

The logical unit in which files are written is the byte. You can split the
bytes into 8 bits...

>>> def bits(f):
.... while True:
.... b = f.read(1)
.... if not b: break
.... b = ord(b)
.... for i in range(8):
.... yield b & 1
.... b >>= 1
....
>>> with open("tmp.dat", "wb") as f: # create a file with some example data
.... f.write(chr(0b11001010)+chr(0b10101111))
>>> with open("tmp.dat", "rb") as f:
.... for bit in bits(f):
.... print bit
....
0
1
0
1
0
0
1
1
1
1
1
1
0
1
0
1

but that's a very inefficient approach. If you explain what you are planning
to do we can most certainly come up with a better alternative.

Peter
From: Richard Thomas on
On Jun 7, 10:17 am, Peter Otten <__pete...(a)web.de> wrote:
> Alfred Bovin wrote:
> > I'm working on something where I need to read a (binary) file bit by bit
> > and do something depending on whether the bit is 0 or 1.
>
> > Any help on doing the actual file reading is appreciated.
>
> The logical unit in which files are written is the byte. You can split the
> bytes into 8 bits...
>
> >>> def bits(f):
>
> ...     while True:
> ...             b = f.read(1)
> ...             if not b: break
> ...             b = ord(b)
> ...             for i in range(8):
> ...                     yield b & 1
> ...                     b >>= 1
> ...>>> with open("tmp.dat", "wb") as f: # create a file with some example data
>
> ...     f.write(chr(0b11001010)+chr(0b10101111))>>> with open("tmp.dat", "rb") as f:
>
> ...     for bit in bits(f):
> ...             print bit
> ...
> 0
> 1
> 0
> 1
> 0
> 0
> 1
> 1
> 1
> 1
> 1
> 1
> 0
> 1
> 0
> 1
>
> but that's a very inefficient approach. If you explain what you are planning
> to do we can most certainly come up with a better alternative.
>
> Peter

You're reading those bits backwards. You want to read the most
significant bit of each byte first...

Richard.
From: Ulrich Eckhardt on
Ulrich Eckhardt wrote:
> data = f.read()
> for byte in data:
> for i in range(8):
> bit = 2**i & byte
> ...

Correction: Of course you have to use ord() to get from the single-element
string ("byte" above) to its integral value first.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932