Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing a bit not working

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
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:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top