I have a function that tests to see if a bit in a number is set. I converted it from VB but it doesn't seem to work in C#.
The error I get is:
Cannot implicitly convert type long to bool.
In vb, which works:
My C# code, which has the error in both the lines that "ands" the Number variable:
I had to change the "Number" to be of type Int64 in my other bit handling function and that worked fine.
Why doesn't the BitIsSet work?
Thanks,
Tom
The error I get is:
Cannot implicitly convert type long to bool.
In vb, which works:
Code:
'*----------------------------------------------------------*
'* Name : BitIsSet *
'*----------------------------------------------------------*
'* Purpose : Test if bit 0 to bit 31 is set *
'*----------------------------------------------------------*
Public Function BitIsSet(ByVal Number As Integer, _
ByVal Bit As Integer) As Boolean
BitIsSet = False
If Bit = 31 Then
If Number And &H80000000 Then BitIsSet = True
Else
If Number And (2 ^ Bit) Then BitIsSet = True
End If
End Function
My C# code, which has the error in both the lines that "ands" the Number variable:
Code:
//*----------------------------------------------------------*
//* Name : BitIsSet *
//*----------------------------------------------------------*
//* Purpose : Test if bit 0 to bit 31 is set *
//*----------------------------------------------------------*
public bool BitIsSet(Int64 Number, int Bit)
{
bool functionReturnValue = false;
functionReturnValue = false;
if (Bit == 31)
{
if (Number & 0x80000000)
functionReturnValue = true;
}
else
{
if (Number & Convert.ToInt64(Math.Pow(2, Bit)))
functionReturnValue = true;
}
return functionReturnValue;
}
I had to change the "Number" to be of type Int64 in my other bit handling function and that worked fine.
Code:
//*----------------------------------------------------------*
//* Name : BitSet *
//*----------------------------------------------------------*
//* Purpose : Sets a given Bit in Number *
//*----------------------------------------------------------*
public long BitSet(Int64 Number, int Bit)
{
if (Bit == 31)
{
Number = 0x80000000 | Number;
}
else
{
Number = (Convert.ToInt64(Math.Pow(2, Bit))) | Number;
}
return Number;
}
Why doesn't the BitIsSet work?
Thanks,
Tom