在 C++11 中,根据 en.cppreference.com ,
For signed and non-negative a, the value of a << b is a * 2b if it is representable in the return type, otherwise the behavior is undefined.
我的理解是,因为 255 * 224 不是
表示为 int32_t ,评价(int32_t) 255 << 24
产生未定义的行为。那是对的吗?这可以吗
编译器依赖?如果重要的话,这是一个 IP16 环境。
背景:这来自an argument I am having与 arduino.stackexchange.com 上的用户。在他看来,“没有什么 对此根本没有定义”:
you notice that much of the bit shifting is "implementation defined". So you cannot quote chapter-and-verse from the specs. You have to go to the GCC documentation since that is the only place that can tell you what actually happens. gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Shifting - it's only "undefined" for a negative shift value.
编辑:从目前的答案来看,我对 C++11 标准 是正确的。那么我的问题的关键部分是 此表达式调用 gcc 中的未定义行为。正如达夫马克所说 在他的评论中,我问“GCC,一个实现,是否定义了一个 行为,即使它没有被语言标准定义。
从我链接到的 gcc 手册来看,它似乎确实被定义了, 虽然我觉得这本手册的措辞听起来更像是教程 而不是“语言法”。来自 PSkocik 的回答(以及凯恩对此的评论 答案),它反而看起来是未定义的。所以我还是有疑问。
我想我的梦想是在一些 gcc 中有一个明确的声明 说明 1) gcc 没有定义任何行为的文档 在标准中明确未定义,或者 2) gcc 确实定义了这个 来自版本 XX.XX 的行为并 promise 在所有版本中保持它的定义 后续版本。
编辑 2:PSkocik 删除了他的答案,我觉得这很不幸,因为 它提供了有趣的信息。从他的回答来看,凯恩对 答案和我自己的实验:
(int32_t)255<<24使用 clang 编译时会产生运行时错误
和 -fsanitize=undefined -fsanitize=undefined (int32_t)256<<24编译时确实会出现运行时错误
g++ -std=c++11 -fsanitize=undefined 第 2 点与 gcc 的解释一致,在 C++11 模式下,
比标准更广泛地定义左移。根据第 3 点,
这个定义可能只是 C++14 的定义。然而,第3点是
与the referenced manual 的想法不一致是一个
<< 的完整定义在 gcc(C++11 模式)中,正如该手册提供的那样
没有暗示 (int32_t)256<<24可以是未定义的。
最佳答案
这随着时间的推移而改变,并且有充分的理由,所以让我们回顾一下历史。请注意,在所有情况下,只需执行 static_cast<int>(255u << 24)一直是定义的行为。也许只是这样做并回避所有问题。
原文C++11措辞是:
The value of
E1 << E2isE1left-shiftedE2bit positions; vacated bits are zero-filled. IfE1has an unsigned type, the value of the result isE1×2<sup>E2</sup>, reduced modulo one more than the maximum value representable in the result type. Otherwise, ifE1has a signed type and non-negative value, andE1×2E2is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
255 << 24在 C++11 中是未定义的行为,因为结果值无法表示为 32 位有符号整数,它太大了。
这种未定义的行为会导致一些问题,因为 constexpr 必须诊断未定义的行为 - 因此一些常见的设置值的方法会导致硬错误。因此 CWG 1457 :
The current wording of 8.8 [expr.shift] paragraph 2 makes it undefined behavior to create the most-negative integer of a given type by left-shifting a (signed) 1 into the sign bit, even though this is not uncommonly done and works correctly on the majority of (twos-complement) architectures [...] As a result, this technique cannot be used in a constant expression, which will break a significant amount of code.
这是针对 C++11 的缺陷。从技术上讲,符合标准的 C++11 编译器将实现所有缺陷报告,因此可以正确地说,在 C++11 中,这不是未定义的行为; 255 << 24 的行为在 C++11 中被定义为 -16777216 .
缺陷后的写法可见C++14 :
The value of
E1 << E2isE1left-shiftedE2bit positions; vacated bits are zero-filled. IfE1has an unsigned type, the value of the result isE1×2<sup>E2</sup>, reduced modulo one more than the maximum value representable in the result type. Otherwise, ifE1has a signed type and non-negative value, andE1×2<sup>E2</sup>is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value; otherwise, the behavior is undefined.
C++17 中的措辞/行为没有变化。
但对于 C++20,由于 Signed Integers are Two's Complement (及其 wording paper ),写法是 greatly simplified :
The value of
E1 << E2is the unique value congruent toE1×2<sup>E2</sup>modulo2<sup>N</sup>, whereNis the range exponent of the type of the result.
255 << 24仍然在 C++20 中定义了行为(具有相同的结果值),只是我们如何到达那里的规范变得简单得多,因为语言不必解决有符号整数的表示是实现定义的。
关于c++ - (int32_t) 255 << 24 是 gcc (C++11) 中的未定义行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813553/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我想学习一些关于Continuation的知识,使用callcc方法从一些文章中键入几个示例,但我遇到了错误:NoMethodError:undefinedmethod`callcc'formain:Objectfrom(pry):2:in`'没有文章提到包含延续库。那么如何解决这个问题呢?谢谢编辑:ruby1.9.2p290(2011-07-09修订版32553)[x86_64-linux] 最佳答案 您需要要求“继续”。require'continuation' 关于ruby-继续,
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是: