Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

Following code is in C++. What would be C# Equivalent Code for this.

C#
int lGpiValue = 512;
int sensor=3;

if( lGpiValue & (1<<sensor))  // What will be in c#
return 1;


Please help.
Posted
Updated 21-Dec-14 3:29am
v2

1 solution

In c# you need an explicit boolean expression in an if-statement.
So you need to compare the result of lGpiValue & (1 << sensor) with a value.
For example:
C#
int lGpiValue = 512;
int sensor = 3;

if ((lGpiValue & (1 << sensor)) != 0) // The exact comparison is for the OP to decide
    return 1;

// Alternative comparison
if (Convert.ToBoolean(lGpiValue & (1 << sensor)))
    return 1;
 
Share this answer
 
v4
Comments
Philippe Mori 20-Dec-14 18:04pm    
It should be !=0. Your code might fail if sensor is 31 and lGpiValue is negative. Or you have to use unsigned numbers.
George Jonsson 20-Dec-14 22:31pm    
You have a good point, but I merely presented an example.
I changed the code as you suggested.
Philippe Mori 21-Dec-14 8:20am    
!= 0... Not 1.
Either, you should give an "unrelated" example or give equivalent code as otherwise OP might think that the code can be used as it... Or put a comment to indicate that it might not be exact.
George Jonsson 21-Dec-14 8:33am    
Thanks. The hamsters were there again. ;)
Kornfeld Eliyahu Peter 21-Dec-14 8:38am    
Maybe Convert.ToBoolean will work better than compare to 0...

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