是什么原因高级语言如C#/ Java的掩盖位移位计数操作数? [英] What's the reason high-level languages like C#/Java mask the bit shift count operand?

查看:116
本文介绍了是什么原因高级语言如C#/ Java的掩盖位移位计数操作数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个多语言设计的,而不是一个编程的问题。

This is more of a language design rather than a programming question.

以下是从 JLS 15.19移位操作符

如果左侧操作数升级后的类型为 INT ,只有右手操作的五个最低阶位被用作移位距离。

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance.

如果左侧操作数升级后的类型为,那么只有右侧操作数的六个最低阶位被用作移位距离。

If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance.

此行​​为也是在C#指定的,而我不敢肯定,如果它在Javascript的官方规格(如果有一个),这也是事实根据我自己的测试最少。

This behavior is also specified in C#, and while I'm not sure if it's in the official spec for Javascript (if there's one), it's also true based on my own test at least.

其结果是,以下为真:

(1 << 32) == 1

我明白,这说明很可能是灵感由底层硬件只需要5位为计数操作的事实移32位值(和6位为64位)的时候,我能理解这样的行为JVM级别,例如指定的,但为什么会高级语言如C#和Java保留此相当低级别的行为?难道他们不应该提供超越硬件实现一个更抽象的观点和行为更直观? (即使更好,如果他们可以采取一个负计数意味着在其他方向移动!)

I understand that this specification is probably "inspired" by the fact that the underlying hardware only takes 5 bits for the count operand when shifting 32-bit values (and 6 bits for 64-bit), and I can understand such behavior specified at the JVM level for example, but why would high level languages such as C# and Java retain this rather low-level behavior? Shouldn't they provide a more abstract view beyond the hardware implementation and behave more intuitively? (Even better if they can take a negative count to mean to shift in the OTHER direction!)

推荐答案

Java和C#不完全高水平。他们试图硬实是这样,他们可以被编译成高效的code,要想在微基准测试的光芒。这就是为什么他们有值类型,如 INT ,而不是具有作为默认的整数类型,真正的整数,这将是他们自己的权利对象,不限到一个固定的范围内。

Java and C# are not fully "high-level". They try real hard to be such that they can be compiled into efficient code, in order to shine in micro-benchmarks. This is why they have the "value types" such as int instead of having, as default integer type, true integers, which would be objects in their own right, and not limited to a fixed range.

因此​​,它们模仿硬件做什么。他们修剪了一下,他们的任务的遮蔽,但在C ++只允许它。不过,Java和C#是中等级别的语言。

Hence, they mimic what the hardware does. They trim it a bit, in that they mandate masking, whereas C only allows it. Still, Java and C# are "medium-level" languages.

这篇关于是什么原因高级语言如C#/ Java的掩盖位移位计数操作数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆