gpt4 book ai didi

c# - 为什么 C# 的二元运算符总是返回 int 而不管其输入格式如何?

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:07 25 4
gpt4 key购买 nike

如果我有两个byteab,怎么会:

byte c = a & b;

产生有关将 byte 转换为 int 的编译器错误?即使我在 ab 前进行显式转换,它也会这样做。

此外,我知道 this question ,但我真的不知道它在这里如何应用。这似乎是 operator &(byte operand, byte operand2) 的返回类型的问题,编译器应该能够像任何其他运算符一样进行排序。

最佳答案

Why do C#'s bitwise operators always return int regardless of the format of their inputs?

我总是不同意。这行得通并且 a & b 的结果是 long 类型:

long a = 0xffffffffffff;
long b = 0xffffffffffff;
long x = a & b;

如果一个或两个参数是longulonguint,则返回类型不是int .


Why do C#'s bitwise operators return int if their inputs are bytes?

byte & byte 的结果是一个 int,因为没有在 byte 上定义 & 运算符。 ( Source )

int 存在一个 & 运算符,并且还有一个从 byteint 的隐式转换,所以当你写 byte1 & byte2 这和写 ((int)byte1) & ((int)byte2) 实际上是一样的,结果是一个 int

关于c# - 为什么 C# 的二元运算符总是返回 int 而不管其输入格式如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com