Mail Archives: djgpp/2000/01/23/06:01:32
hey david and everyone,
David Cleaver wrote:
> OK, I'll explain a little 'bit' more. :) I'm writing a program to
> factor numbers, hopefully, very quickly. Now, in order to do this I
> have created a bunch of two dimensional arrays whose elements only
> consist of zero's and one's. Since this is the case, I decided to
> reduce the size of the array from using an int for each zero or one, and
> have packed all the zero's and one's into unsigned char's. As you can
> see, this greatly reduced the amount of storage needed....
i think there is a better way to do it that wouldn't really matter what
type you used for each element. but say you used an unsigned char, which is
8-bits..you could store 8-bits {funnily enough} in one char, and access
them using the bit-shift (>> and <<) operators. the binary arithmetic takes
a bit of getting used to, but it will mean you aren't wasting *any* space,
except for a few bits that might be unused in the very last char.
i'm not sure it's useful, but i have code that defines "bitstrings" that
provide operations to extend by a bit, and to take bits from the stream.
these can only have length up to the size of an unsigned long, but there
is also code for "bitstreams", that can write/read a series of bits to/from
a file.
the bitstrings aren't all that space-efficient, because each one has to store
how many bits are currently in use, but the bitstreams do not need this, as they
only actually write to the file after each unsigned long is full of information.
you could tailor the bitstream code so that it uses yr 2D-array rather then a
file. this would take 8-times less space then yr current approach {assuming
i understand it properly} :)
if yr interested in this approach, u can e-mail me for more information.
if you would rather not use my code, i'm sure u could do it all with the
bitshift operators :)
hope this helps,
daniel.
- Raw text -