为什么一起添加2个字节会产生一个int? [英] why does adding 2 bytes together result in an int?

查看:57
本文介绍了为什么一起添加2个字节会产生一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是一个字节的最大值是255.因此,为什么

会导致以下代码出现编译错误?


byte val1 = 10;

byte val2 = 23;

byte ttl;

ttl = val1 + val2; //这行不会编译


编译错误:无法将类型''int''隐式转换为''byte''。


val1 + val2 = 33仍然在一个字节的范围内。因此,为什么我需要将$ btl转换为int?


谢谢。



-
mo*******@nospam.nosp am

解决方案



moondaddy写道:

我的理解是一个字节的最大值是255.因此,为什么以下代码会出现编译错误?

字节val1 = 10;
字节val2 = 23;
字节ttl;
ttl = val1 + val2; //这行不会编译

编译错误:无法将类型''int''隐式转换为''byte''。

val1 + val2 = 33仍然在一个字节的范围。因此,为什么我需要将ttl转换为int?




什么是128 + 128?


-

Larry Lard

回复团体请


你好,moondaddy!


m> byte val1 = 10;

m> byte val2 = 23;

m> byte ttl;

m> ttl = val1 + val2; //这行不会编译


m>编译错误:无法将类型''int''隐式转换为''byte''。


m> val1 + val2 = 33仍然在一个字节的范围内。因此,为什么

m>我是否需要将ttl转换为int?


IMO问题在于IL操作码''add''不使用byte(int8)类型来执行添加。这就是结果值为int32的原因。


如果你会写


ttl =(字节)(val1 + val2)

然后''转化添加了.u1''操作码,将值转换为neede类型(int8或byte)...


-

问候,Vadym Stetsyak

www: http://vadmyst.blogspot.com

我知道这是关于代码优化和.NET背后的技术的讨论,但我认为在这种情况下显式转换是个好主意

原则问题。


对一个字节进行数学运算是一种特殊的事情,所以投射会让你

仔细查看可能的代码很快遇到麻烦。更好的是,

它还具有创建自我记录代码的额外好处 - 你必须是一个真正的ninny,不知道为什么代码''作者正在从一个int中输入一个字节并且



恕我直言的显式转换使得该值的严重限制对其他人显而易见

程序员维护你的代码(当你花了五年时间给你自己
忘了你在做什么!)


Carlo

" Vadym Stetsyak" < VA ***** @ ukr.net>在消息中写道

news:ud ************** @ TK2MSFTNGP15.phx.gbl ...

你好,moondaddy!

m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //这行不会编译

m>编译错误:无法将类型''int''隐式转换为''byte''。

m> val1 + val2 = 33仍然在一个字节的范围内。因此,为什么
m>我需要将ttl转换为int吗?
IMO这里的麻烦是IL操作码''add''不使用byte(int8)
类型来执行添加。这就是为什么结果值是int32。

如果你要写的话

ttl =(byte)(val1 + val2)
然后''conv.u1' '添加了操作码,将值转换为neede类型(
int8或byte)...

- 问候,Vadym Stetsyak
www: http://vadmyst.blogspot.com



my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type ''int'' to ''byte''.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do I
need to convert ttl into an int?

Thanks.


--
mo*******@nospam.nospam

解决方案


moondaddy wrote:

my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type ''int'' to ''byte''.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do I
need to convert ttl into an int?



What''s 128 + 128 ?

--
Larry Lard
Replies to group please


Hello, moondaddy!

m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //this line wont compile

m> compile error: Cannot implicitly convert type ''int'' to ''byte''.

m> val1 + val2 = 33 which is still in the range of a byte. therefore, why
m> do I need to convert ttl into an int?

IMO the trouble here is that IL opcode ''add'' doesn''t use byte ( int8 ) type to perform additions. That is why the result value is int32.

if you will write

ttl = (byte)(val1 + val2)
then ''conv.u1'' opcode is added that converts the value to neede type ( int8 or byte )...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


I know that this was a discussion on code optimisation and the technology
behind .NET, but I think that explicit casting in this case is a good idea
as a matter of principle.

Performing math on a byte is an exceptional affair, so casting makes you
look carefully at code that could very quickly run into trouble. Better yet,
it has the added benefit of creating self-documenting code - you''d have to
be a real ninny not to know why the code''s author was casting a byte to and
from an int.

IMHO explicit casting makes the value''s severe limitation obvious to other
programmers maintaining your code (and to yourself in five years when you
forgot what you were doing!)

Carlo
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:ud**************@TK2MSFTNGP15.phx.gbl...

Hello, moondaddy!

m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //this line wont compile

m> compile error: Cannot implicitly convert type ''int'' to ''byte''.

m> val1 + val2 = 33 which is still in the range of a byte. therefore, why
m> do I need to convert ttl into an int?

IMO the trouble here is that IL opcode ''add'' doesn''t use byte ( int8 )
type to perform additions. That is why the result value is int32.

if you will write

ttl = (byte)(val1 + val2)
then ''conv.u1'' opcode is added that converts the value to neede type (
int8 or byte )...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com



这篇关于为什么一起添加2个字节会产生一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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