A bit vector is just an array of bits; subsets of bits within the bytes have some meaning. That allows more compact storage for certain types of data. For example, if you only needed boolean on-off values, you can store eight values in one byte. If your values require more than byte sized bits, the bits you require can still be packed more efficiently; if you need 10 bits, you can pack eight of those in an ten byte string (rather than the 16 or 32 bytes you might otherwise use).
The use of vector in this context probably came from jump tables: the bits represent a place for code to jump to, and therefore are at least vaguely related to the physics/engineering definition of a vector quantity (maginitude and direction). I still think of these as bit maps or bit fields, but apparently I'm out of touch.
Perl has the "vec" function for bit fields. Its granularity is a little bit limited: the number of bits you want to examine or set has to be a power of 2, so you can't (for example) conveniently work with three bit fields. You'd need to use four bits, a small waste, or handle all the nasty details yourself with substr and << >> operators. Using "vec" is a lot more pleasant. Here's an example that sets some individual bits, and reads them back from the string.
#!/usr/bin/perl
my $bitf;
vec($bitf,0,1)=1;
vec($bitf,1,4)=7;
for($x=0;$x < 8;$x++) {
print "$x ",vec($bitf,$x,1), "\n";
}
# another way to print out a bit field
print unpack("b*", $bitf), "\n";
That produces:
0 1 1 0 2 0 3 0 4 1 5 1 6 1 7 0 10001110
If you don't see why setting bits 4 to 7 as 7 produces "110", it's just simple banary, starting from bit 4: bit 4 has the value of 1 if set, bit 5 has 2, bit 6 has 4. See Javascript Bit Twiddler
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar