Reputation: 327
How can we bit select the variables in perl code?
I am new to perl and I have a scenario where I need to extract a particular format from a file and give input to a another module for analysis.
Currently I have extracted the required pattern which is in 16-bit Hexadecimal. Now from this 16bits hexadecimal format ,I want only LSB 10bits. Kindly refer the below example(This is a sample code where I have used only 1 line of my requirement)
use strict;
my $string = "HDR 0c0d PlD 1000 GAP 412";
$string =~ s/.*HDR\s(\S+).*/$1/g;
print "$string\n";
my $hex = hex($string);
print "$hex";
The output in $hex
is 3095 which is 16bit 16’b0011000010000101
now I need to extract only the LSB 10bits(0010000101
), Please let me know some easy way to do this .
Upvotes: 2
Views: 395
Reputation: 8220
Use bit mask to select bits which you need. To select right 10 bit you can use:
my $x = 0xfff0;
print $x & 0x3ff;
output is
1008
which is decimal number of ten bits of number 0xfff0
Upvotes: 5