I feel stupid asking this question, and i'll probably kick myself if i see the solution. I need to determine whether a number is a power of two (2, 4, 8, 16, 32, etc.). Can someone please help.
Printable View
I feel stupid asking this question, and i'll probably kick myself if i see the solution. I need to determine whether a number is a power of two (2, 4, 8, 16, 32, etc.). Can someone please help.
Use this example of logaritmics:
[Edited by kedaman on 05-30-2000 at 02:23 PM]Code:if Log(value) / Log(2) = int(Log(value) / Log(2))then
msgbox value & "is 2^" & Log(value) / Log(2)
end if
may as well post it even though kedaman beat me to it!
Code:Option Explicit
Private Sub Command1_Click()
Dim retval As Double
retval = Log2(Val(Text1))
If retval = Int(retval) Then
MsgBox "number is a power of 2"
Else
MsgBox "number is not a power of 2"
End If
End Sub
Static Function Log2(X)
Log2 = Log(X) / Log(2#)
End Function
I feel stupid. Thanx guys.
I thought of Log and checking if value = Int(Value).
I just didn't know how to put it all together.
Never could understand this Log stuph.
You're right about that, i don't like your function. ok i'm making a isbase2 one here:
Code:Function isbase2(value) as boolean
isbase2 = Log(value) / Log(2) = int(Log(value) / Log(2))
End Function