in reply to Bitwise Operator Error

Strings used in numerical context as treated as decimal numbers. Use hex to convert them

Similarly, numerical literals are treated as decimal numbers. Prepend 0x.

#!/usr/bin/perl -w # File displayPipeTracer2.pl my $testString = " 00008008 000000FF 00800000"; $testString =~ s/\s+//g; my $maskResult = hex($testString) & 0x400000000000000000000000; print "Mask Result: $maskResult\n";

But those numbers are huge!

Integer overflow in hexadecimal number at script.pl line 7. Hexadecimal number > 0xffffffff non-portable at script.pl line 7. Integer overflow in hexadecimal number at script.pl line 7. Hexadecimal number > 0xffffffff non-portable at script.pl line 7. Mask Result: 4294967295

But Perl is capable of large number arithmetic using bigint/Math::BigInt.

#!/usr/bin/perl -w # File displayPipeTracer2.pl use bigint; my $testString = " 00008008 000000FF 00800000"; $testString =~ s/\s+//g; $testString = Math::BigInt->new("0x$testString"); my $maskResult = $testString & 0x400000000000000000000000; print "Mask Result: $maskResult\n";

Replies are listed 'Best First'.
Re^2: Bitwise Operator Error
by ravishi (Acolyte) on Oct 03, 2008 at 13:35 UTC
    Thanks, that worked. However, this is just a sample code. I tried using bigint in my main program and it gives me a whole slew of new errors. It is a GUI program using Tk and a few Tk modules (Tk::TableMatrix, Tk::Balloon, Tk::Bitmap, and Tk::Pane). I'm guessing that bigint and the Tk modules conflict. These are two separate errors I get when I run it with bigint. The second I get when I comment out the first error.
    C:\code>DisplayPipeTracer15.pl -trace out.PipeTracer.xml '8┬¿├⌐☻♥' isn't numeric at C:/Perl/site/ +lib/Tk/Widget.pm line 205. at C:\code\displayPipeTracer15.pl line 387 C:\code>DisplayPipeTracer15.pl -trace out.PipeTracer.xml trace_file = out.PipeTracer.xml Ports to be Analyzed: 1 2 'h┬Ñ├⌐☻♥' isn't numeric at C:/Perl/site/ +lib/Tk.pm line 250. at C:\code\displayPipeTracer15.pl line 159
    Here is my code for the lines the errors point to. First is 387 and the second is 159.

    Line 387:

    my $cycleAccept = $statusBar->Button(-text => "Go to", -font => ['Arial', 8], -command=> \&GoToCycle, -bd => 1, -takefocus => 'never', -highlightthickness => 0)->pack( +-side => 'left');

    Line 159:

    $PortFrame = $allPortsFrame->Scrolled('TableMatrix', -resizeborders=>'none', -titlerows => 1, -titlecols => 1, -background => 'white', -foreground => 'white', -borderwidth => 0, -rows => 6, -colstretchmode=>'all', -cols => 1, -colwidth => 25, -cache => 1, -scrollbars => "osoe")->pa +ckAdjust(-side => "top", + -fill => 'both', + -expand => 1);
      bigint basically turns every number into an object which acts as a number. Limit the scope of bigint using curlies, or create the objects yourself were needed.
      #!/usr/bin/perl -w # File displayPipeTracer2.pl { # Only makes objects from numbers in these curlies use bigint; my $testString = " 00008008 000000FF 00800000"; $testString =~ s/\s+//g; $testString = Math::BigInt->new("0x$testString"); my $maskResult = $testString & 0x400000000000000000000000; print "Mask Result: $maskResult\n"; }
      #!/usr/bin/perl -w # File displayPipeTracer2.pl my $testString = " 00008008 000000FF 00800000"; $testString =~ s/\s+//g; $testString = Math::BigInt->new("0x$testString"); my $mask = Math::BigInt->new('0x400000000000000000000000'); my $maskResult = $testString & $mask; print "Mask Result: $maskResult\n";