免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2607 | 回复: 9
打印 上一主题 下一主题

&操作符处理字符串 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-14 12:29 |只看该作者 |倒序浏览
#!/usr/bin/perl -w


my $a = "1010";
my $b = "111";

print ($a & $b, "\n");


我看书上说,如果两个字符串不一样长,那么将短的后边补0,这个结果为101,不大明白为什么这个结果

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
2 [报告]
发表于 2009-02-14 16:21 |只看该作者
原帖由 rt77789 于 2009-2-14 12:29 发表
#!/usr/bin/perl -w


my $a = "1010";
my $b = "111";

print ($a & $b, "\n");

我看书上说,如果两个字符串不一样长,那么将短的后边补0,这个结果为101,不大明白为什么这个结果

在哪看的?

perldoc perlop
...while the & op acts as though the longer operand were truncated to the length of the shorter...

论坛徽章:
0
3 [报告]
发表于 2009-02-14 17:08 |只看该作者

回复 #2 ynchnluiti 的帖子

啊,我在大骆驼上看的。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2009-02-14 21:21 |只看该作者
原帖由 rt77789 于 2009-2-14 17:08 发表
啊,我在大骆驼上看的。

你误解那里的讲解了。原话是这样:
If both operands arestrings (and have not been used as numbers since they were
set), the operators do bitwise operations between corresponding bits from the two
strings. In this case, there’s no arbitrary limit, since strings aren’t arbitrarily limited
in size. If one string is longer than the other, the shorter string is considered to
have a sufficient number of 0 bits on the end to make up the differ ence.

注意补的是 0 bit,而不是字符 '0';而且对于字符串,& 做的是 bitwise and。

[ 本帖最后由 MMMIX 于 2009-2-14 21:29 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-02-14 21:31 |只看该作者

回复 #4 MMMIX 的帖子

啊,我理解错了,谢谢你~我看的是翻译的,貌似这里翻译的有些问题。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2009-02-14 21:31 |只看该作者
原帖由 ynchnluiti 于 2009-2-14 16:21 发表

在哪看的?

perldoc perlop

perlop 对 & 的讲解和 Programming Perl 的讲解并不冲突,只不过 Programming Perl 的讲解是从实现角度说的,而 perlop 的讲解是从语义角度说的。

论坛徽章:
0
7 [报告]
发表于 2009-02-14 21:49 |只看该作者
原帖由 MMMIX 于 2009-2-14 21:21 发表

你误解那里的讲解了。原话是这样:

注意补的是 0 bit,而不是字符 '0';而且对于字符串,& 做的是 bitwise and。

#!/usr/bin/perl -w



my $a = "\1\0\1\0";
my $b = "\1\1\1";

for (unpack("C*", $a & $b)) {
    print ;
}
print "\n";

结果输出是101,我不大明白为啥是这个结果呢,前辈能不能讲解下

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
8 [报告]
发表于 2009-02-14 22:19 |只看该作者
原帖由 rt77789 于 2009-2-14 21:49 发表

  1. #!/usr/bin/perl -w

  2. my $a = "\1\0\1\0";
  3. my $b = "\1\1\1";

  4. for (unpack("C*", $a & $b)) {
  5.     print ;
  6. }
  7. print "\n";
复制代码

结果输出是101,我不大明白为啥是这个结果呢

假设你的代码保存在 t.pl 中,那么你可以用

  1. perl -MO=Deparse  ./t.pl
复制代码

看看 perl 是如何理解你的代码的。
至于最后的结果,看看 Perl 的 print 1; 打印的是什么你就清楚了。

论坛徽章:
0
9 [报告]
发表于 2009-02-14 22:52 |只看该作者

回复 #8 MMMIX 的帖子

我总认为结果为1010...

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
10 [报告]
发表于 2009-02-15 12:10 |只看该作者
原帖由 rt77 于 2009-2-14 22:52 发表
我总认为结果为1010...

Programming Perl 在 "Bitwise Operators" 这一小节的描述不太准确,perlop 的描述就要严格的多:
       If the operands to a binary bitwise op are strings of different sizes,
       | and ^ ops act as though the shorter operand had additional zero bits
       on the right, while the & op acts as though the longer operand were
       truncated to the length of the shorter.  The granularity for such
       extension or truncation is one or more bytes.

碰到问题最好还是查手册,书和手册不一致的地方,以手册为准。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP