Results 1 to 5 of 5

Thread: Power of 2...

  1. #1

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Question

    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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
    Mark
    -------------------

  4. #4

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Talking 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

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width