|
-
May 29th, 2000, 06:08 PM
#1
Thread Starter
Fanatic Member
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.
r0ach™
Don't forget to rate the post
-
May 29th, 2000, 06:21 PM
#2
transcendental analytic
Use this example of logaritmics:
Code:
if Log(value) / Log(2) = int(Log(value) / Log(2))then
msgbox value & "is 2^" & Log(value) / Log(2)
end if
[Edited by kedaman on 05-30-2000 at 02:23 PM]
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 29th, 2000, 06:22 PM
#3
Frenzied Member
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
-
May 29th, 2000, 06:32 PM
#4
Thread Starter
Fanatic Member
I knew it!
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.
r0ach™
Don't forget to rate the post
-
May 29th, 2000, 06:33 PM
#5
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|