简体   繁体   English

为什么将较大变量的类型转换为较小的变量会导致较大变量的模数为较小变量的范围

[英]Why Type Casting of larger variable to smaller variable results in modulo of larger variable by the range of smaller variable

Recently,while I was going through typecasting concept in java, I have seen that type casting of larger variable to smaller variable results in the modulo of larger variable by the range of smaller variable.Can anyone please explain this in detail why this is the case and is it true for any explicit type conversion?.最近,当我在java中进行类型转换概念时,我看到将较大变量类型转换为较小变量会导致较大变量与较小变量的范围相乘。谁能详细解释一下为什么会这样任何显式类型转换都是如此吗?

     class conversion {
    public static void main(String args[]) 
    {
        double a = 295.04;
        int  b = 300;
        byte c = (byte) a;
        byte d = (byte) b;
        System.out.println(c + " "  + d);
    } 
}

The above code gives the answer of d as 44 since 300 modulo 256 is 44.Please explain why this is the case and also what happens to the value of c?上面的代码给出了 d 的答案为 44,因为 300 模 256 是 44。请解释为什么会这样,以及 c 的值会发生什么变化?

It is a design decision that goes all the way back the the C programming language and possibly to C's antecedents too.这是一个设计决策,可以追溯到 C 编程语言,也可能追溯到 C 的前身。

What happens when you convert from a larger integer type to a smaller integer type is that the top bits are lopped off.当您从较大的整数类型转换为较小的整数类型时,会发生高位被删除的情况。

Why?为什么? Originally (and currently) because that is what hardware integer instructions support.最初(和当前)因为这是硬件整数指令所支持的。

The "other" logical way to do this (ie NOT the way that Java defines integer narrowing) would be to convert to that largest (or smallest) value representable in the smaller type;执行此操作的“其他”逻辑方式(即不是 Java 定义整数缩小的方式)将转换为可在较小类型中表示的最大(或最小)值; eg例如

    // equivalent to real thin in real java
    // b = (byte) (Math.max(Math.min(i, 127), -128))

would give +127 as the value of b .将给出 +127 作为b的值。 Incidentally, this is what happens when you convert a floating-point value to an integer value, and the value is too large.顺便说一句,当您将浮点值转换为整数值并且值太大时会发生这种情况。 That is what is happening in your c example.这就是您的c示例中发生的情况。


You also said:你还说:

The above code gives the answer of d as 44 since 300 modulo 256 is 44.上面的代码给出了 d 的答案为 44,因为 300 模 256 是 44。

In fact, the correct calculation would be:事实上,正确的计算应该是:

int d = ((b + 128) % 256) - 128;

That is because the range of the Java byte type is -128 to +127.那是因为Java byte类型的范围是-128到+127。


For completeness, the above behavior only happens in Java when the larger type is an integer type.为了完整起见,上述行为仅在 Java 中较大类型是整数类型时才会发生。 If the larger type is a floating point type and the smaller one is an integer type, then a source value that is too large or too small (or an infinity) gets converted to the largest or smallest possible integer value for the target type;如果较大的类型是浮点类型,而较小的类型是整数类型,则太大或太小(或无穷大)的源值将转换为目标类型的最大或最小可能整数值; eg例如

double x = 1.0e200;
int i = (int) x;   // assigns 'Integer.MAX_VALUE' to 'i'

And a NaN is converted to zero.并且 NaN 被转换为零。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM