devxlogo

Power2 – A power of 2

Power2 – A power of 2

' Raise 2 to a power' the exponent must be in the range [0,31]Function Power2(ByVal exponent As Long) As Long    Static res(0 To 31) As Long    Dim i As Long        ' rule out errors    If exponent  31 Then Err.Raise 5        ' initialize the array at the first call    If res(0) = 0 Then        res(0) = 1        For i = 1 To 30            res(i) = res(i - 1) * 2        Next        ' this is a special case        res(31) = &H80000000    End If        ' return the result    Power2 = res(exponent)        End Function

devx-admin

Share the Post: