Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int crc_register ;


if (crc_register & 0x0001)
{
crc_register = crc_register >> 1;
crc_register = crc_register ^ 0xa001;
}
else
{
crc_register = crc_register >> 1;
}

What I have tried:

Cannot implicitly convert type 'int' to 'bool'
Posted

1 solution

When you find code on the internet in one language and try to use it in another, you have to understand three things:
1) The source language - in this case C or C++
2) The destination language - in this case C#
3) What the code does, and how it does it.

C and C++ look a lot like C# (understandable, C# was heavily influenced by them when it was designed) but they are very, very different languages which share some common syntax. You can't just lift C code and assume it will work unchanged in C#!

In this case, C does not have a concept of boolean values - no true or false. Instead, it treats any non-zero value as true, and any zero value as false. C# does have boolean values, and enforces their use.
So this:
if (crc_register & 0x0001)
needs to be changed to this:
if ((crc_register & 0x0001) != 0)

But ... you cannot expect the code even when it compiles to work exactly the same as the original unless you comply with all three of the conditions above!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900