十六进制常数 [英] Hexadecimal Constants

查看:140
本文介绍了十六进制常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据其十六进制表示形式声明一个整数参数.之间有什么区别

I want to declare an integer parameter based on its hexadecimal representation. What are the differences between:

INTEGER(kind=int32), PARAMETER :: a = Z'FFFFFFFF'
INTEGER(kind=int32), PARAMETER :: b = int(Z'FFFFFFFF', kind=int32)
INTEGER(kind=int32), PARAMETER :: c = transfer(Z'FFFFFFFF', 1_int32)

(是的,我知道这只是-1.)

(And yes, I know that this is just -1.)

gfortran似乎在编译期间给了我一个整数溢出错误(有帮助地告诉我,可以使用-fno-range-check忽略它),但对于c却没有.

gfortran seems to give me an integer overflow error during compile (helpfully telling me that I can ignore that with -fno-range-check) for the above a and b, but not for c.

我需要使其与Fortran 2003兼容,因为此代码可能是由其他地方的不同编译器编译的.

I need to make it Fortran 2003 compliant, as this code might be compiled with different compilers elsewhere.

推荐答案

第一和第三条语句不是有效的Fortran. boz文字常量只能出现在多个有限的上下文中-int内在常量是这些上下文之一.

The first and third statements are not valid Fortran. A boz literal constant can only appear in a number of limited contexts - the int intrinsic being one of those contexts.

中间语句根据boz-literal-constant指定的位序列将命名常量的值设置为处理器相关的值.该值取决于处理器,因为结果值中的最高有效位是1.

The middle statement sets the value of the named constant to a processor dependent value based on the sequence of bits specified by the boz-literal-constant. The value is processor dependent because the most significant bit in the resulting value is one.

使用Fortran 2008规则进行详细说明(弗拉基米尔指出,Fortran 2003有所不同):

Elaborating, using the Fortran 2008 rules (Fortran 2003 was different, as Vladimir notes):

  • boz文字常量指定一个32位(或on/.TRUE./whatever)位的序列.
  • INTEGER(INT32)指定一个STORAGE_SIZE为32位的整数,该整数可能大于或等于该类型对象的BIT_SIZE(存储位和值位"可能由于对齐要求而有所不同) ).
  • 如有必要,INT内部函数将位序列截断为相关的位大小.
    • 如果该截断序列的最左位为零,则INT内部值由类似SUM([b(i) * (i-1)**2, i = 1, SIZE(b)])的值给出,其中b是代表位序列的数组,最右边的位在b(1)中
    • 如该示例所示,如果该截断序列的最右边一位是1,则标准表示结果取决于处理器.这是为了适应使用值的内部表示中的最高有效位表示符号的典型做法.使用非常常见的整数的二进制补码表示法,您将获得值-1.
    • The boz literal constant specifies a sequence of 32 one (or on/.TRUE./whatever) bits.
    • INTEGER(INT32) specifies an integer with a STORAGE_SIZE of 32 bits, which is presumably greater than or equal to the BIT_SIZE of an object of that type (storage bits and "value bits" may be different due to things like alignment requirements).
    • If necessary, the INT intrinsic truncates the bit sequence to the relevant bit size.
      • If the left most bit of that truncated sequence was zero, the value of the INT intrinsic is as given by something like SUM([b(i) * (i-1)**2, i = 1, SIZE(b)]) where b is an array representing the bit sequence, with the rightmost bit being in b(1).
      • If the right most bit of that truncated sequence is one, as in the example, the standard says the result is processor dependent. That's to accommodate the typical practice of using the most significant bit in the internal representation of a value to represent the sign. With the very common two's complement representation of integers, you'll get a value of -1.

      在Fortran 2003中,使用处理器上可用的最大整数表示,将位序列解释为正数.结果值将超出INTEGER(INT32)对象的范围,从而使代码不符合要求.

      Under Fortran 2003 the sequence of bits is interpreted as a positive number using the largest integer representation available on the processor. The resulting value will be out of the range of an INTEGER(INT32) object, making the code non-conforming.

      这篇关于十六进制常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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