扩展运算符+(加号)为字节/短类型 [英] Extending operator + (plus) for byte/short type

查看:80
本文介绍了扩展运算符+(加号)为字节/短类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道今天的字节很便宜,但是当
i知道所计算的实体不是更大时,
i仍然更喜欢使用一个字节(或短)

比255(或65k)。然而,每次执行操作时都会施展真正的痛苦




示例:

私有字节a ,b;

公共字节MyProperty

{get {return(byte)(a + b); } $

我怎么能避免这样做?我想

重新安装/重载arithmetical

运算符,这样它们在计算两个字节时返回一个字节(或短)

(或短裤)。


这很容易吗?可行吗?


-

问候

Konrad Viltersten

----- ---------------------------

睡觉 - 为穷人替代咖啡

野心 - 缺乏懒惰感

解决方案

K Viltersten写道:


虽然我知道今天的字节很便宜,但当我知道实体

算得上时,
i仍然更喜欢使用一个字节(或短)不大于

比255(或65k)。然而,每次执行操作时都会施展真正的痛苦




示例:

私有字节a ,b;

公共字节MyProperty

{get {return(byte)(a + b); } $

我怎么能避免这样做?我想重新声明/重载

算术运算符,以便在计算两个字节(或短路)时返回一个字节(或短)




这很容易吗?它可行吗?



我不认为它是可行的。


你应该改变你的编码风格并使用int。节省空间不仅节省了不小的费用,而且最终可能会花费更多的代价来支持CPU周期和内存。


Arne


K Viltersten写道:


虽然我知道今天的字节很便宜,但是>
i仍然更喜欢使用一个字节(或短)当我知道实体

计数不大于
而不是255(或65k)。然而,每次执行操作时都会施展真正的痛苦




示例:

私有字节a ,b;

公共字节MyProperty

{get {return(byte)(a + b); } /



它*应该是一种痛苦。你可以很好地了解CPU必须做的工作。


对于32位CPU,32位整数的运行效率比
$ b $高得多b按字节操作。你必须使用字节的唯一原因是为了节省内存的b
,但你必须要非常紧张才能证明处理中的

减速。而且你必须证明你实际上是在节省*

内存:操作字节需要更多指令,而且这些指令也需要内存



关于您遇到字节的唯一时间是字节数组。单个字节

转换为整数以进行任何重要操作,并且它们应该保持

,直到你将它们存回。


公共属性应该基本上不会小于int,它不会支付b $ b $(除非,再次,你可以证明你真的需要内存)或什么时候

它们是预定义互操作结构的一部分。


使用短或字节或字节强制执行范围是一个坏主意:在一个强制转换中很容易抛出

来强制值在范围内,但这实际上是否产生了正确的
结果?在setter中明确检查范围是一个更好的想法。


我怎么能避免这样做?我想重新声明/重载

算术运算符,以便在计算两个字节(或短路)时返回一个字节(或短)




不能用C#完成。事实上,它不能用任何.NET语言来完成,我很想知道。那些操作映射到CLR原语并且从

语言重载它们将是非常奇怪的 - 更不用说低效了。


-

J.


Arne Vajh?j写道:


K Viltersten写道:


>虽然我知道今天的字节很便宜,但是当我知道实体被计算时,我仍然更喜欢使用一个字节(或短)并不比255(或65k)大。



即127或32767.两种类型都是签名的。


>然而,每次执行操作时都会施展真正的痛苦。

示例:
私有字节a,b;
public byte MyProperty
{get {return(byte)(a + b); }

我怎么能避免这样做?我想重新声明/重载
算术运算符,以便在计算两个字节(或短路)时返回一个字节(或短)

它是否容易实现?它可行吗?



我不认为这是可行的。


你应该改变你的编码风格并使用int。

空间不仅节省了不小的价值,而且你可能最终花费更多的资金来支付代码的CPU周期和内存。



除非你有阵列,否则我怀疑使用byte保存没有空间

或短或短 int的内容。它们可能是在32位边界上分配的b $ b,就像更大的类型一样,

未使用的24或16位只是浪费了。


While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when
i know that the entity counted isn''t larger
than 255 (or 65k). However, it''s a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I''d like to
redeclare/overload the arithmetical
operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

解决方案

K Viltersten wrote:

While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn''t larger
than 255 (or 65k). However, it''s a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I''d like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?

I don''t think it is doable.

You should change your coding style and use int. Not only is the space
saving insignificant, but you potentially could end up spending more
CPU cycles and memory for code.

Arne


K Viltersten wrote:

While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn''t larger
than 255 (or 65k). However, it''s a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

It *should* be a pain. You get a good indication of the work the CPU has to do.

Operating on 32-bit integers is vastly more efficient for a 32-bit CPU than
operating on bytes. The only reason you could have to use bytes is to save
memory, but you have to be pretty tight on that before you can justify the
slowdown in processing. And you have to show that you''re actually *saving*
memory: more instructions are needed to manipulate bytes, and those take up
memory too.

About the only time you encounter bytes is in byte arrays. Individual bytes
are converted to ints for any nontrivial operation, and they should stay
that way until you store them back.

Public properties should basically never be smaller than an int, it doesn''t
pay (unless, again, you can show that you really need the memory) or when
they''re part of a predefined interop structure.

Using "short" or "byte" to enforce ranges is a bad idea: it''s easy to throw
in a cast to force the value to be in range, but does this actually produce
the correct result? Checking the range explicitly in the setter is a better
idea.

How can i avoid doing that? I''d like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Can''t be done in C#. In fact, it can''t be done in any .NET language I''m
aware of. Those operations map to CLR primitives and overloading them from a
language would be decidedly strange -- not to mention inefficient.

--
J.


Arne Vajh?j wrote:

K Viltersten wrote:

>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn''t larger
than 255 (or 65k).

That is, 127 or 32767. Both types are signed.

>However, it''s a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I''d like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?


I don''t think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending more
CPU cycles and memory for code.

Unless you''ve got arrays of them, I suspect that there''s no space
saved by using "byte" or "short" insead of "int". They''re probably
allocated on 32-bit boundaries, just like the larger types, with the
unused 24 or 16 bits simply wasted.


这篇关于扩展运算符+(加号)为字节/短类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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