Поразрядно или: C# по сравнению с C++

Не делать.

Пираты будут пират. Неважно, что решение Вы придумываете, оно может и взламываться.

, С другой стороны, Ваши фактические, платящие клиенты - те, кому причиняет беспокойство дерьмо.

5
задан Axonn 12 December 2009 в 23:24
поделиться

3 ответа

The & and | operators in C# are the same as in C/C++. For instance, 2 | 8 is 10 and 2 & 8 is 0.

The difference is that an int is not automatically treated like a boolean value. int and bool are distinct types in C#. You need to compare an int to another int to get a bool.

if (2 & 8) ...         //  doesn't work

if ((2 & 8) != 0) ...  //  works
14
ответ дан 18 December 2019 в 06:02
поделиться

It's not the bitwise and/or operators that are different in C#. They work almost the same as in C++. The difference is that in C# there isn't an implicit conversion from integer to a boolean.

To fix your problem you just need to compare to zero:

if ((a | b) != 0) {
   ...
8
ответ дан 18 December 2019 в 06:02
поделиться

Unlike C/C++, C# maintains a pretty strict separation between arithmetic and boolean operations.

The designers of C# considered the automatic conversion of integral types to boolean to be a source of errors that they'd rather C# didn't have, so you have to explicitly make your arithmetic results to a boolean result by introducing a comparison:

if ((a | b) != 0) {
    // ...
}

I think it's probably not a bad idea to do this in C/C++ as well, but I'll admit that I certainly don't strictly follow that advice (and I wouldn't argue for it very hard).

5
ответ дан 18 December 2019 в 06:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: