【问题标题】:Casting Primitive Values铸造原始值
【发布时间】:2021-06-08 20:01:45
【问题描述】:

值:1,921,222,太大了,不能存储为short,所以出现数值溢出,变成20,678。

谁能演示 1,921,222 变成 20,678 的过程? 如何“环绕”到下一个最低值并从那里向上计数以获得 20,678 提前谢谢你

【问题讨论】:

    标签: overflow underflow


    【解决方案1】:

    在 C 语言中,“short”类型有 2 个字节。每个整数值都被编译器视为 32 位或 4 字节“int”类型(这可能因编译器而异)。

     short s = 1921222;
    

    在这句话中,您丢失了 2 个字节的数据:

                    Information that remains in the variable (2 bytes)
                          ^         ^
     00000000 00011101 01010000 11000110   ->  total data (4 bytes, 32 bits)
        v        v
     Information discarded when you put this value in a short type.
    

    换句话说,您“裁剪”数据,只留下符合指定类型的部分。

    01010000 11000110
    

    “01010000 11000110”是 20678。

    本网站可以帮助您更好地了解此过程的工作原理: https://hexed.it/

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多