Reputation: 67
I am reading some script which is written in Perl and I don't understand it. I have never used Perl before. I've read someting about scalars and that confuses me. For an example look at this code:
my $sim_packets = 5;
my $sim_length = $payload_length * 2 * $sim_packets;
push @data, (1...$sim_length * 10);
my $data_32bit = 0;
if I use after this:
$data_32bit = shift @data;
What is length of $data_32bit
in bits?
I ask this because I have another array in this code: @payload
, and this line confuses me:
push @payload, ($data_32bit >> 24) & 0xff,
($data_32bit >> 16) & 0xff,
($data_32bit >> 8) & 0xff,
($data_32bit) & 0xff;
$data_32bit
is 32 bit long?
Upvotes: 1
Views: 902
Reputation: 386541
If I understand correctly third line of this code created array: 1..50?
It added to an array rather than creating one.
The scalars added consist of numbers starting with 1 and going up to and including $payload_length * 2 * $sim_packets * 10
, which is $payload_length * 100
.
$payload_length
is unlikely to be 1/2, so I suspect the number of scalars added is more than the 50 you mentioned.
What is length of $data_32bit in bits?
What does that even mean?
The size of the scalar of 24 bytes on one of my system.
$ perl -MDevel::Size=total_size -E'$i=123; say total_size $i'
24
The amount of bits required to store the value:
ceil(log($payload_length * 100) / log(2))
In this case, the author appears to be indicating the value will/should fit in 32 bits. That will be the case unless $payload_length
exceeds some number larger than 40,000,000.
and this line confuses me:
It adds four values to the array. The four values correspond to the bytes of $data_32bit
when stored as a unsigned two's complement number with the most significant byte first.
Upvotes: 2
Reputation: 36339
Oh boy,
push @payload, ($data_32bit >> 24) & 0xff, ($data_32bit >> 16) & 0xff, ($data_32bit >> 8) & 0xff, ($data_32bit) & 0xff;
Somebody apparently needs to
perldoc -f pack
perldoc -f unpack
Regarding your question, $data_32bit
is not 32bit long, just becasue the term 32bit
appears in its name. If you need to know how exactly it is represented, you should go for Data::Dumper
.
Perl stores integers in the mantissa of a native floating point number, so it really depends on the machine architecture. With IEEE, it should be something like 53 bits.
Upvotes: 3