Results 1 to 6 of 6

Thread: Decomposing the sum of the powers of 2

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Question Decomposing the sum of the powers of 2

    Hi,

    I'd like to use the concept of summing the powers of number 2 to combine settings in my app.
    Kind of the same as VB uses vbNo, vbYes, vbIgnore etc

    After I sum these numbers the challenge is to determine which numbers have been used in the sum.
    Eg.
    Setting #1 --> 2^1=2
    Setting #2 --> 2^2=4
    Setting #3 --> 2^3=8

    If user selects #1 & #3 that sums up to 10 (2+8) --> this 10 can only be constructed from 2 and 8, no other combination of 2,4 and 8 is possible.
    But how to deterrmine that with a function?

    In the past I have been using a built-in function that gave me TRUE/FALSE if asked whether a certain number is a part of the this kind of sum or not, but cannot find it.

    So:
    is 2 a part of 10 (where 10 is a sum of 2^n) = TRUE
    is 4 a part of 10 (where 10 is a sum of 2^n) = FALSE
    is 8 a part of 10 (where 10 is a sum of 2^n) = TRUE


    Any help would be appreciated.

    Bostjan

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Decomposing the sum of the powers of 2

    Not sure what you are asking for?

    Using standard bit-wise operations...
    If (10 And 2) Then Msgbox "2 is part of 10" Else MsgBox "2 is not part of 10"

    If wanting to know each bit set in the number, high bit (&H80000000) needs to be tested separately
    Code:
    Dim lMask As Long
    Dim lTarget As Long
    
       lTarget = 2018  ' <<< number to test; negative numbers contain high bit
    
       lMask = &H40000000
       Do Until lMask = 0
          If (lTarget And lMask)  Then Debug.Print "number includes: " lMask
          lMask = lMask \ 2
       Loop
    Last edited by LaVolpe; Jan 18th, 2018 at 10:37 AM. Reason: oops, left of a zero on the starting mask
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Decomposing the sum of the powers of 2

    And here is another solution that can produce less loops, but rewrites the target number in the process. Same restriction, high bit needs to be tested separately.
    Code:
    Dim lMask As Long
    Dim lTarget As Long
    
        lTarget = 2018  ' <<< number to test; negative numbers contain high bit
        Do Until lTarget = 0
            lMask = lTarget And -lTarget: Debug.Print "number includes: "; lMask
            lTarget = lTarget Xor lMask
        Loop
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Decomposing the sum of the powers of 2

    It just looks like bits to me. However, one thing that's being overlooked is that:

    Setting #? --> 2^0=1

    It really looks like it should be:

    Setting #1 --> 2^0=1
    Setting #2 --> 2^1=2
    Setting #3 --> 2^2=4
    Setting #3 --> 2^3=8

    Or, if you wanted to do it thinking about the bits, as counting (say in a byte, numbered 1,2,3,4,5,6,7,8), you could do:

    Setting --> 2^(bit-1) = a number that can be used as a mask for this "bit" (using AND or OR or XOR, etc).

    Also, be careful in that any operation with a power is going to return a Double. So, to use this idea with Integers or Longs, you may want to convert:

    Setting --> CInt(2^(bit-1))
    Setting --> CLng(2^(bit-1))

    Also, for Integers, you could only go to a power of 14 and Longs could only go to a power of 30, or you start tangling with the sign-bit, which is another discussion.

    Maybe That'll Help,
    Elroy
    Last edited by Elroy; Jan 18th, 2018 at 12:11 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Decomposing the sum of the powers of 2

    Normally you'd be better off using integer multiply/divide and masking as described.

    But there are also the "Driver Support Routines" in the NT Kernel that could be used. While meant for longer Byte arrays they work with non-Variant value types in VB6. Here is an example using a single Long as the buffer:

    Name:  sshot.png
Views: 156
Size:  4.6 KB
    Attached Files Attached Files

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Re: Decomposing the sum of the powers of 2

    Thanks to all! THis really helps!
    Much appreciated.

    Bostjan

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