[svn:perlfaq] r10876 - perlfaq/trunk
[email protected] Mon, 3 Mar 2008 11:49:36 -0800 (PST)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog
Date: Mon Mar 3 11:49:36 2008
New Revision: 10876
Modified:
perlfaq/trunk/perlfaq4.pod
Log:
* perlfaq4: How do I manipulate arrays of bits?
+ once again I am an idiot. Save the file before committing
changes! This is the content for the last commit.
+ Steffen Heinrich pointed out a mismatch between the prose
for the first example and what the example already does.
+ I fixed the code, and elaborated in the prose
Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod (original)
+++ perlfaq/trunk/perlfaq4.pod Mon Mar 3 11:49:36 2008
@@ -1723,16 +1723,23 @@
Use C<pack()> and C<unpack()>, or else C<vec()> and the bitwise
operations.
-For example, this sets C<$vec> to have bit N set if C<$ints[N]> was
-set:
+For example, you don't have to store individual bits in an array
+(which would mean that you're wasting a lot of space). To convert an
+array of bits to a string, use C<vec()> to set the right bits. This
+sets C<$vec> to have bit N set only if C<$ints[N]> was set:
+ @ints = (...); # array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... )
$vec = '';
foreach(@ints) {
vec($vec,$_,1) = 1 if $_;
}
-Here's how, given a vector in C<$vec>, you can get those bits into your
-C<@ints> array:
+The string C<$vec> only takes up as many bits as it needs. For
+instance, if you had 16 entries in C<@ints>, C<$vec> only needs two
+bytes to store them (not counting the scalar variable overhead).
+
+Here's how, given a vector in C<$vec>, you can get those bits into
+your C<@ints> array:
sub bitvec_to_list {
my $vec = shift;