• <bdo id='IYKrY'></bdo><ul id='IYKrY'></ul>
    <legend id='IYKrY'><style id='IYKrY'><dir id='IYKrY'><q id='IYKrY'></q></dir></style></legend>
  1. <small id='IYKrY'></small><noframes id='IYKrY'>

    <i id='IYKrY'><tr id='IYKrY'><dt id='IYKrY'><q id='IYKrY'><span id='IYKrY'><b id='IYKrY'><form id='IYKrY'><ins id='IYKrY'></ins><ul id='IYKrY'></ul><sub id='IYKrY'></sub></form><legend id='IYKrY'></legend><bdo id='IYKrY'><pre id='IYKrY'><center id='IYKrY'></center></pre></bdo></b><th id='IYKrY'></th></span></q></dt></tr></i><div id='IYKrY'><tfoot id='IYKrY'></tfoot><dl id='IYKrY'><fieldset id='IYKrY'></fieldset></dl></div>
    1. <tfoot id='IYKrY'></tfoot>

      byte + byte = int ... 为什么?

      byte + byte = int... why?(byte + byte = int ... 为什么?)
      <legend id='0l3xW'><style id='0l3xW'><dir id='0l3xW'><q id='0l3xW'></q></dir></style></legend>

      <small id='0l3xW'></small><noframes id='0l3xW'>

    2. <i id='0l3xW'><tr id='0l3xW'><dt id='0l3xW'><q id='0l3xW'><span id='0l3xW'><b id='0l3xW'><form id='0l3xW'><ins id='0l3xW'></ins><ul id='0l3xW'></ul><sub id='0l3xW'></sub></form><legend id='0l3xW'></legend><bdo id='0l3xW'><pre id='0l3xW'><center id='0l3xW'></center></pre></bdo></b><th id='0l3xW'></th></span></q></dt></tr></i><div id='0l3xW'><tfoot id='0l3xW'></tfoot><dl id='0l3xW'><fieldset id='0l3xW'></fieldset></dl></div>

        <bdo id='0l3xW'></bdo><ul id='0l3xW'></ul>
          <tbody id='0l3xW'></tbody>
              <tfoot id='0l3xW'></tfoot>
                本文介绍了byte + byte = int ... 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                看看这段 C# 代码:

                Looking at this C# code:

                byte x = 1;
                byte y = 2;
                byte z = x + y; // ERROR: Cannot implicitly convert type 'int' to 'byte'
                

                byte(或 short)类型执行的任何数学运算的结果都会隐式转换回整数.解决方案是将结果显式转换回一个字节:

                The result of any math performed on byte (or short) types is implicitly cast back to an integer. The solution is to explicitly cast the result back to a byte:

                byte z = (byte)(x + y); // this works
                

                我想知道为什么?它是建筑的吗?哲学?

                What I am wondering is why? Is it architectural? Philosophical?

                我们有:

                • int + int = int
                • long + long = long
                • float + float = float
                • double + double = double
                • int + int = int
                • long + long = long
                • float + float = float
                • double + double = double

                为什么不呢:

                • 字节 + 字节 = 字节
                • short + short = short?
                • byte + byte = byte
                • short + short = short?

                一点背景知识:我正在对小数"(即 < 8)执行一长串计算,并将中间结果存储在一个大数组中.使用 byte 数组(而不是 int 数组)更快(因为缓存命中).但是通过代码传播的大量字节转换使其更加难以阅读.

                A bit of background: I am performing a long list of calculations on "small numbers" (i.e. < 8) and storing the intermediate results in a large array. Using a byte array (instead of an int array) is faster (because of cache hits). But the extensive byte-casts spread through the code make it that much more unreadable.

                推荐答案

                你的代码片段的第三行:

                The third line of your code snippet:

                byte z = x + y;
                

                其实就是

                byte z = (int) x + (int) y;
                

                因此,对字节没有 + 操作,字节首先转换为整数,两个整数相加的结果是一个(32 位)整数.

                So, there is no + operation on bytes, bytes are first cast to integers and the result of addition of two integers is a (32-bit) integer.

                这篇关于byte + byte = int ... 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <tbody id='cVWke'></tbody>

                  • <tfoot id='cVWke'></tfoot>
                      <bdo id='cVWke'></bdo><ul id='cVWke'></ul>
                    • <small id='cVWke'></small><noframes id='cVWke'>

                        <legend id='cVWke'><style id='cVWke'><dir id='cVWke'><q id='cVWke'></q></dir></style></legend>

                        1. <i id='cVWke'><tr id='cVWke'><dt id='cVWke'><q id='cVWke'><span id='cVWke'><b id='cVWke'><form id='cVWke'><ins id='cVWke'></ins><ul id='cVWke'></ul><sub id='cVWke'></sub></form><legend id='cVWke'></legend><bdo id='cVWke'><pre id='cVWke'><center id='cVWke'></center></pre></bdo></b><th id='cVWke'></th></span></q></dt></tr></i><div id='cVWke'><tfoot id='cVWke'></tfoot><dl id='cVWke'><fieldset id='cVWke'></fieldset></dl></div>