[Pdx-pm] bit conversion brain-teaser

Joshua Hoblitt jhoblitt at ifa.hawaii.edu
Mon Mar 3 21:16:11 CST 2003


I have a 7-bit digital I/O module that outputs the state of it's channels via an ascii hex value.

For example:

normal state		: 7F
channel 0 to V-		: 7E
channel 6 to V-		: 3F
.
.

What I need to get out of this is the state of each channel.  I thought it would be pretty simple to do a hex -> per bit conversion but I ended up resorting to to heavy weapons.  This is my solution:

use Bit::Vector;

my $vector = Bit::Vector->new_Hex( '7', "7F" );

for ( 0 .. 6 ) {
        print $vector->contains( $_ ), "\n";
}

This probably shows that I'm ignorant of bit fiddling in general but it seems to me there should be a simple way to do this with unpack?  Or a way to treat a binary int like a stack and pop each bit off?

Anyways I thought some of you might find this a fun problem to solve.

Cheers,

-J

--




More information about the Pdx-pm-list mailing list