bool int

  • Thread starter Thread starter Elementary Penguin
  • Start date Start date
E

Elementary Penguin

Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;
 
Or...

if (true) ++count;

....which is arguably an order of magnitude more readable anyway.
Afterall, I'm still not sure if you wrote the number 1 or the letter l.

Brian
 
Gee, you must be an ex-C hack like me!

C was one of the few languages that got ints and bools confused. You
couldn't do that in Pascal, either!

C# is strongly typed: booleans and integers are different things. I
prefer it, but that's just me. :)
 
Let me give you more background.

I wanted to count bits on type byte, and I found a c routine:

http://www.caam.rice.edu/~dougm/twiddle/BitCount.html

int count = 0;
int tmpbits = bits;
while (tmpbits) {
if (tmpbits & 1) ++count;
tmpbits >>= 1;
}
return count;

but to turn it into a c# method requires:

int cbits(byte b)
{
int count = 0;
int tmpbits = b;
while (tmpbits>0)
{
if ((tmpbits & 1)==1) ++count;
tmpbits >>= 1;
}

return count;
}


Brian said:
Or...

if (true) ++count;

...which is arguably an order of magnitude more readable anyway.
Afterall, I'm still not sure if you wrote the number 1 or the letter l.

Brian
 
Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

If requires a condition that evaluates to either true or false. 1 does not
evaluate true or false. You can either go with your second line, or:

if(true) ++count;

For us former C/C++ developers, we can also do:

if(Convert.ToBoolean(1)) ++count;

Convert.ToBoolean(short/int/long/double/decimal) overloads return true if
the value is non-zero, otherwise false.
 
I see where you're coming from now.

So the ole bit count eh? Here is what I used recently for a chess
engine in C#.

public int BitCount(UInt64 bitboard)
{
int count;
for (count = 0; bitboard != 0; bitboard &= bitboard - 1;) count++;
return count;
}

This is one function I was hoping would be implemented by the framework
because the JIT compiler could output the faster x86 instructions for
bit counting if the application was running on a CPU that supported
those instructions.

Brian
 
no, because that's infinitely worse.

for example, if I intend to do if(i == x) but wrote if( i = x ), in your
scenario, it will still run, but will become a hard to track bug.
 
Worse? Not at all, if( i = x ) won't even compile as the expression is of
type int and if expects a bool.

Willy.
 
Willy Denoyette said:
Worse? Not at all, if( i = x ) won't even compile as the expression is of
type int and if expects a bool.

No, that's exactly what Daniel was saying. In the kind of situation the
OP was talking about (where if(1) compiles), for instance in C, you get
the type of problem Daniel mentioned.
 
Jon Skeet said:
No, that's exactly what Daniel was saying. In the kind of situation the
OP was talking about (where if(1) compiles), for instance in C, you get
the type of problem Daniel mentioned.

Sure, I didn't notice OP and Danial were refering to C.

Willy.
 
Back
Top