-1

In JavaScript we can do:

if(blahblah & 1)

But in C#, it's not possible to do:

if(blahblah & 1)

Compilation Error:

cannot convert blahblah to boolean

So how can we represent bitwise operator as an if comparison in C#?

As comments pointed out, I am trying to get the same result as that code:

function readUIntV(buffer) {
    // 1 Byte
    if (buffer[0] & 1) {
        return {
            messageSize: buffer[0] >> 1,
            end: 1
        }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ragedcoder
  • 57
  • 11
  • 6
    Well, what *Boolean* condition are you trying to test for? My guess is that you *may* want `if ((blahblah >> 1) != 0)` but I can't tell for sure. Fundamentally, the condition for an `if` statement has to be a Boolean expression... so work out what you're trying to test, and write that expression. – Jon Skeet Jul 16 '21 at 10:54
  • Okay, so with the new example *what are you trying to test*, in a way that evaluates to "true" or "false"? `buffer[0] & 1` presumably evaluates to an integer - so what *Boolean* expression are you trying to evaluate based on that integer? What results would you expect to evaluate to true, and which would you expect to evaluate to false? – Jon Skeet Jul 16 '21 at 11:00

3 Answers3

2

JavaScript has a concept of "truthy" and "falsy" - as long as it "has some value", it is true as far as a test (such as in an if) is concerned. C# is much more strict: a test has to evaluate to a boolean value (true or false) and a bitwise operator returns a int.

Solution: test it against an int value, for instance

if((blahblah & 1) != 0)

Note that because of operator precedence, you need the extra ( ) - otherwise you would get a compiler error:

Operator '&' cannot be applied to operands of type 'int' and 'bool'

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0
[Flags]
public enum Example
{
    Val1 = 1,
    Val2 = 2,
    Val3 = 4
}

var test = Example.Val2;
if ((Example.Val1 & test) == test) 
{
    // do
}

Reference

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
  • If the target enum type has the `[Flags]` attribute you might as well take advantage of [`HasFlag()`](https://learn.microsoft.com/en-us/dotnet/api/system.enum.hasflag): `if(test.HasFlag(Example.Val1)) ...` – Mathias R. Jessen Jul 16 '21 at 11:47
0

From Bitwise AND (&) - JavaScript | MDN:

The bitwise AND operator (&) returns a 1 in each bit position for which the corresponding bits of both operands are 1s.

Furthermore,

The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32 bit integer.

Since the result is an integer, javascript evaluates the truthiness of the value instead. (See Truthy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN)

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).

JavaScript uses type coercion in Boolean contexts.

Therefore, if (buffer[0] & 1) is "if the rightmost bit of buffer[0] is ON" in English OR

C#:

if((blahblah & 1) != 0)

xGeo
  • 2,149
  • 2
  • 18
  • 39