C# 中的 OR-ing 字节给出 int [英] OR-ing bytes in C# gives int

查看:18
本文介绍了C# 中的 OR-ing 字节给出 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.

byte dup = 0;
Encoding.ASCII.GetString(new byte[] { (0x80 | dup) });

当我尝试编译时,我得到:

When I try to compile I get:

不能隐式转换类型'int'到'字节'.显式转换存在(你缺少演员吗?)

Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

为什么会这样?不应该|两个字节给出一个字节?以下两项工作,确保每个项目都是一个字节.

Why does this happen? Shouldn't | two bytes give a byte? Both of the following work, assuring that each item is a byte.

Encoding.ASCII.GetString(new byte[] { (dup) });
Encoding.ASCII.GetString(new byte[] { (0x80) });

推荐答案

C# 就是这样设计的,事实上,可以追溯到 C/C++ - 后者也将操作数提升为 int,你通常不会注意到,因为 int ->char 转换是隐式的,而 C# 中没有.这不仅适用于 | ,也适用于所有算术和按位操作数 - 例如添加两个 byte 也会给你一个 int .我将在此处引用规范的相关部分:

It's that way by design in C#, and, in fact, dates back all the way to C/C++ - the latter also promotes operands to int, you just usually don't notice because int -> char conversion there is implicit, while it's not in C#. This doesn't just apply to | either, but to all arithmetic and bitwise operands - e.g. adding two bytes will give you an int as well. I'll quote the relevant part of the spec here:

二进制数字提升发生在预定义 +、-、的操作数*、/、%、&、|、^、==、!=、>、<、>= 和 <= 二元运算符.二进制数字提升隐式转换两个操作数都为一个公共类型,在非关系的情况下运算符,也成为结果操作的类型.二进制数字推广包括应用遵循规则,按照他们的顺序出现在这里:

Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

  • 如果任一操作数是输入十进制,另一个操作数是转换为十进制类型,或编译时错误发生,如果另一个操作数的类型为 float 或 double.

  • If either operand is of type decimal, the other operand is converted to type decimal, or a compile-time error occurs if the other operand is of type float or double.

否则,如果任一操作数是输入double,另一个操作数是转换为 double 类型.

Otherwise, if either operand is of type double, the other operand is converted to type double.

否则,如果任一操作数是浮点型,另一个操作数被转换为类型浮动.

Otherwise, if either operand is of type float, the other operand is converted to type float.

否则,如果任一操作数是 ulong 类型,另一个操作数是转换为 ulong 类型,或编译时错误发生,如果另一个操作数的类型为 sbyte、short、int,或长.

Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.

否则,如果操作数的类型为 long,另一个操作数转换为 long 类型.

Otherwise, if either operand is of type long, the other operand is converted to type long.

否则,如果任一操作数是类型 uint 和另一个操作数是输入 sbyte、short 或 int,两者都可以操作数转换为 long 类型.

Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.

否则,如果任一操作数是输入 uint,另一个操作数是转换为 uint 类型.

Otherwise, if either operand is of type uint, the other operand is converted to type uint.

否则,两个操作数都转换为类型内部.

Otherwise, both operands are converted to type int.

我不知道这样做的确切理由,但我可以考虑一个.特别是对于算术运算符,人们可能会感到 (byte)200 + (byte)100 突然等于 44 可能有点令人惊讶,即使这在某些情况下是有意义的仔细考虑所涉及的类型.另一方面,int 通常被认为是一种对于大多数典型数字的算术足够好"的类型,因此通过将两个参数都提升为 int,您会得到一种大多数常见情况下的正常工作"行为.

I don't know the exact rationale for this, but I can think about one. For arithmetic operators especially, it might be a bit surprising for people to get (byte)200 + (byte)100 suddenly equal to 44, even if it makes some sense when one carefully considers the types involved. On the other hand, int is generally considered a type that's "good enough" for arithmetic on most typical numbers, so by promoting both arguments to int, you get a kind of "just works" behavior for most common cases.

至于为什么这种逻辑也适用于按位运算符 - 我想这主要是为了一致性.它产生了一个对所有非布尔二进制类型通用的简单规则.

As to why this logic was also applied to bitwise operators - I imagine this is so mostly for consistency. It results in a single simple rule that is common for all non-boolean binary types.

但这主要是猜测.至少 Eric Lippert 可能会询问 C# 做出这一决定背后的真正动机(尽管如果答案只是这就是它在 C/C++ 和 Java 中的实现方式,而且已经足够好"的话,那就有点无聊了规则就是这样,所以我们认为没有理由改变它").

But this is all mostly guessing. Eric Lippert would probably be the one to ask about the real motives behind this decision for C# at least (though it would be a bit boring if the answer is simply "it's how it's done in C/C++ and Java, and it's a good enough rule as it is, so we saw no reason to change it").

这篇关于C# 中的 OR-ing 字节给出 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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