• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Bit Vector, Using Perl Vec

By Tony Lawrence | on Jul 19, 2005 | 0 Comment
CGI and Perl Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Bit Vector, Using Perl Vec

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 therefor 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

Share this story:
  • tweet

Author Description

No Responses to “Bit Vector, Using Perl Vec”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.