Perl非常适合做位串/向量.设置位非常简单
vec($bit_string, 123, 1) = 1;
Run Code Online (Sandbox Code Playgroud)
获取设置位的计数很快
$count = unpack("%32b*", $bit_string);
Run Code Online (Sandbox Code Playgroud)
但如果你设置在2_147_483_639之上,你的计数将默默地变为零而没有任何明显的警告或错误.
有没有办法解决?
以下代码演示了此问题
#!/usr/bin/env perl
# create a string to use as our bit vector
my $bit_string = undef;
# set bits a position 10 and 2_000_000_000
# and the apparently last valid integer position 2_147_483_639
vec($bit_string, 10, 1) = 1;
vec($bit_string, 2_000_000_000, 1) = 1;
vec($bit_string, 2_147_483_639, 1) = 1;
# get a count of the bits which are set
my $bit_count = unpack("%32b*", $bit_string);
print("Bits set in …Run Code Online (Sandbox Code Playgroud)