简体   繁体   English

c# 字节与运算符

[英]c# byte AND operator

how can I use & operator betwen BYTE and INT?如何在 BYTE 和 INT 之间使用 & 运算符?

it is said here:这里说:

http://int64.org/docs/gamestat-protocols/ase.html http://int64.org/docs/gamestat-protocols/ase.html

however in php its working but in c# not...但是在 php 中它可以工作,但在 c# 中没有...

PHP: PHP:

if ($flag & 1)
    ...
}

c# c#

if (flag & 1)
{
  ...
}

The flag & 1 part is working - but if requires a Boolean expression. flag & 1部分正在工作 - 但if需要Boolean表达式。 So you can do:所以你可以这样做:

if ((flag & 1) != 0)

Note that you have to put parentheses round the & expression as != has higher precedence (binds tighter) than & .请注意,您必须在&表达式周围加上括号,因为!=&具有更高的优先级(绑定更紧密)。

If you're working with a set of flags though, it's usually a better idea to use an enum.但是,如果您正在使用一组标志,那么使用枚举通常是一个更好的主意。 For example, look at BindingFlags - you might use:例如,查看BindingFlags - 您可能会使用:

if ((flags & BindingFlags.Instance) != 0)

That increases type safety (so you know you're trying to compare it with the right kind of flag) and reduces the use of magic numbers.这增加了类型安全性(因此您知道您正在尝试将其与正确类型的标志进行比较)并减少幻数的使用。

Try like this:试试这样:

if ((flag & 1) == 1)
{
    ...
}

The & operator returns an integer in C# whereas if expects a boolean expression. &运算符在 C# 中返回 integer,而if需要 boolean 表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM