[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: manipulating bits not bytes-Enthusiasts level question
- From: David Manura <dm.lua@...>
- Date: Mon, 6 Sep 2010 09:48:15 -0400
On Sat, Sep 4, 2010 at 9:15 AM, Patrick Mc(avery >
> bin = io.open('29fb.bin', 'r') --29fb is 512 KB I am on Linux no "rb"
> bin_all = bin:read("*all")
> bin_zero, ntimes = bin_all.gsub(bin_all, '1', '0')
> print(ntimes) --> 20 --should be way more times then this
If you want to count the number of 1's in binary data, I suggest
creating a lookup table that maps each byte (0-255) to the number of
1's in that byte. Then sum each byte after translating via that
table.
> Is there also a way to read one bit? read(1) is one byte, is there a way to
> read one bit?
The Lua implementation of the DEFLATE algorithm processes bits at a time. [1]
[1] http://lua-users.org/wiki/ModuleCompressDeflateLua