Re: Testing & Using Bits
by Zaxo (Archbishop) on May 25, 2007 at 18:26 UTC
|
Perl has bitwise ops & and |. See the list of bitwise ops in perlop.
BCD can be ginned up with vec and split //.
| [reply] |
Re: Testing & Using Bits
by FunkyMonk (Bishop) on May 25, 2007 at 18:57 UTC
|
Perl has the same bit-banging operators as C, if that's any help. The following should get you going
# powers of two. used to set, reset & test bits
my @bit = map { 1 << $_ } 0 .. 7;
#a number to play with
my $x = 0b10000;
#set bit 2
$x |= $bit[2];
#test bits 2 & 4
print "Y\n" if $x & $bit[2];
print "Y\n" if $x & $bit[4];
#reset bit 4
$x &= ~$bit[4];
#test bit 4 again
print "N\n" unless $x & $bit[4];
$x = 0b10000101; # 0x85
#get low order BCD digit
my $lo = $x & 0b1111;
#get high order BCD digit
my $hi = ( $x & 0b11110000 ) >> 4;
print "$hi $lo\n";
( $hi, $lo ) = ( 2, 5 );
#pack 2 and 5 into byte
$x = $hi << 4 | $lo;
printf "%b\n", $x
| [reply] [d/l] |
Re: Testing & Using Bits
by bobf (Monsignor) on May 25, 2007 at 20:19 UTC
|
If you'd rather work with bits from a higher level (given your comment about the syntax), take a look at Bit::Vector. Specifically, start with the from_Enum, Bit_Off, Bit_On, Norm, and bit_test methods.
| [reply] [d/l] [select] |
Re: Testing & Using Bits
by naikonta (Curate) on May 25, 2007 at 18:32 UTC
|
Check merlyn's article
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
| [reply] |
Re: Testing & Using Bits
by Anonymous Monk on May 25, 2007 at 19:17 UTC
|
Thank you all so very much...I just KNEW it had to be available. Some of the help (syntax)is over my head yet, but I'll work on it. Nice to know there's this safety net available for the COBOL re-trainees among us. Thanks again. | [reply] |
Re: Testing & Using Bits
by Anonymous Monk on May 30, 2007 at 11:13 UTC
|
FunkyMonk, thanks for the insite- I read the posting tips & appreciate the heads up. My last post WAS rather ugly. My problem seems to be threefold- 1. Finding things in the man pages. Where is the Bits:: etc. functions defined & do I need use statements at the beginning of my code to use things like Bits:: or File:: etc.?
2. I've been learning new languages for over 3 decades & it grinds me to have to ask someone else to do my work, but alas "seeking devine guidence", can save time. Thanks for the tip on the tutorials. There was a segment that I stumbled onto that caused me some grief...
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x123
+45678))), "\n";
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",$Byte
+s))), "\n";
This first line works, it prints the 0x12 0x34 etc., but the second just prints 0x00 0x00 etc. I know the 6 hex bytes are in $Bytes because printing it yeilds the proper graphics on the screen. Can you explain the "%#02x"? I've read the printf doc but the light just isn't comming on.
3. See, sorry to ramble on, but when I finally do ask for help, I just want to give you as much info as possible. I work in support, so I know all too well the headaches associated with getting bits (no pun) of info on a "need to know" basis. I guess the next post will have to be "a sample of my script", but I'm trying to avoid burdening you with the laughter. Thanks again.
By the way, I don't plan on spending time here, but if you folks would prefer I sign up, I'd be happy to. At my age, I just try to avoid having to remember additional IDs & passwords. Thanks. 'le Dufois.
| [reply] [d/l] |
|
|
my $bytes = 0x123456;
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x123
+45678))), "\n";
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",$byte
+s))), "\n";
#output:
#0x78 0x56 0x34 0x12
#0x56 0x34 0x12 00
What did you expect it to do? | [reply] [d/l] |
|
|
That's exactly what I expected. I'll recheck my code, but I was getting all zeroes. I even cut/pasted the line & THEN substituted the scalar for the string. Also where can I fine the doc on the sprintf codes %#02x, etc. Thanks.
| [reply] |
|
|
|
|
|
|
Re: Testing & Using Bits
by Anonymous Monk on May 28, 2007 at 23:56 UTC
|
OK, so maybe writing a perl script over the holiday WASN'T my greatest idea, but...(the honey-do list not withstanding) I had a super time. Now it's back to questions...
FunkyMonk- I tried your suggestion & had some success, but it seems I don't get the proper result when I try to use something like my $x = ob01001001. I'm on a windows XP box, would that make a difference? I'm trying to print a 12 byte UPC Code from a 6 byte BCD field. I think I understand the shifting (>>) of the 4 bits, but when I try $hi=($x & 0b11110000).. 4; I think I need to "OR" it with 0x30 inorder to get the ASCII digit. ie 0101 0000 shifted = 0000 0101, then an ASCII digit of 5 would actually be 0x35 or 00110101, am I way off here? Then I would join the 12 bytes into a string for printing. When I OR it all I end up with is the 0x30 or 48dec.
Thanks for the help, & your patience. BTW I have 2 perl books - the Gecko & the Black Panther. I'm thinking I neeed one in between. Does anyone recomend the camel vs llama books (I can only afford ONE of them right now.) Thanks. | [reply] |
|
|
Perl is very flexible with its conversions between numbers and text so there aren't any conversions to ASCII needed:
my $x = 0b01001001;
my $hi = ($x & 0b11110000) >> 4;
$hi = $hi . " (four)";
print "The answer was $hi";
#The answer was 4 (four)
As far as a book recommendation goes, I learnt Perl mostly from The Perl Cookbook. A great book if you have some experience of other languages.
Perl comes with a huge amount of help for learning Perl. If you're running Activestate Perl, you should have all the documentation in html format already installed. Have a poke around in the Start menu. Of course, PerlMonks has a wealth of information too. Have a look in the tutorials section.
If you're going to spend any time here at PM, I think you should read Writeup Formatting Tips ;) | [reply] [d/l] |