the use of & (and) in C#

  • Thread starter Thread starter Kim Schulz
  • Start date Start date
K

Kim Schulz

hi
Im Converting some C source to C# and hit this small problem.
I have a

for(x=0;....x++){

if (x & 1) ....
}
but how do I construct the (x & 1) so it returns a boolean value?
 
Kim,

You want to compare that it is not equal to zero (any value).

Basically, you do this:

if (x & 1 != 0)

This will give you the result that you want.

Hope this helps.
 
Kim Schulz said:
hi
Im Converting some C source to C# and hit this small problem.
I have a

for(x=0;....x++){

if (x & 1) ....
}

C justs tests an if clause for non-zeroness
so

if ( (x&1) != 0 )
{
.. . .
}

David
 
Kim Schulz said:
hi
Im Converting some C source to C# and hit this small problem.
I have a

for(x=0;....x++){

if (x & 1) ....
}
but how do I construct the (x & 1) so it returns a boolean value?

if ((x&1) != 0)
 
Nicholas Paldino said:
You want to compare that it is not equal to zero (any value).

Basically, you do this:

if (x & 1 != 0)

This will give you the result that you want.

Unfortunately you need more bracketting:

if ( (x&1) != 0)

otherwise it tries to bracket it as:

if ( x & (1 != 0))

which is obviously a nonsense.

I'm not sure why the precedence works this way round, to be honest.
 
Jon Skeet said:
Unfortunately you need more bracketting:

if ( (x&1) != 0)

otherwise it tries to bracket it as:

if ( x & (1 != 0))

which is obviously a nonsense.

I'm not sure why the precedence works this way round, to be honest.

I havn't checked the spec, but I would assume the designers just decided to
be right associative. Much of the language works that way and & != probably
have equal precedence. Thus, the right most operation is the correct one.

Changing precendence might have some simplicity benifits, but I don't know
that it wouldn't cause some other weird problems. Also, some parser
generators may only allow associativity changes and changing != or &'s
associativity would be a terrible thing, I think.
 
Jon said:
Unfortunately you need more bracketting:

if ( (x&1) != 0)

otherwise it tries to bracket it as:

if ( x & (1 != 0))

which is obviously a nonsense.

I'm not sure why the precedence works this way round, to be honest.

I have always liked Steve Oualline's advice in "Practical C Programming"
(not that I follow it religiously):

There are fifteen precedence rules in C (&& comes before || comes
before ?:).

The practical programmer reduces these to two:

1) Multiplication and division come before addition
and subtraction.

2) Put parentheses around everything else.
 
Daniel O'Connell said:
I havn't checked the spec, but I would assume the designers just decided
to be right associative. Much of the language works that way and & !=
probably have equal precedence. Thus, the right most operation is the
correct one.

Changing precendence might have some simplicity benifits, but I don't know
that it wouldn't cause some other weird problems. Also, some parser
generators may only allow associativity changes and changing != or &'s
associativity would be a terrible thing, I think.

After checking, != is immediatly more precedent and you are right it is
utterly silly. Since comparisons are booleans, bitwise doesn't really work
that well. There must be some specific issue here.
 
Back
Top