C# 中的按位赋值运算符

发布于 2024-12-18 07:41:13 字数 390 浏览 1 评论 0原文

|=&= 这样的运算符在整数和长整数上用作按位运算符...

int a = 123;
int b = 234;
a |= b;
Console.WriteLine(a); // outputs 251

但在布尔值上,这是一个逻辑运算:

bool a = true;
bool b = false;
a |= b;
Console.WriteLine(a); // outputs true

^ 是如何工作的=&=|= 运算符决定在应用于不同数据类型时使用哪种操作?

Operators like |= and &= work as bitwise operators on ints and longs...

int a = 123;
int b = 234;
a |= b;
Console.WriteLine(a); // outputs 251

But on a bool, it's a logical operation:

bool a = true;
bool b = false;
a |= b;
Console.WriteLine(a); // outputs true

How do the ^=, &= and |= operators decide which manipulation to use when being applied to different data types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-12-25 07:41:13

编译器根据所涉及表达式的静态类型来决定。

The compiler decides, based on the static types of the expressions involved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文