Results 1 to 17 of 17

Thread: [RESOLVED] need formula - unique combination

  1. #1

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290

    Question [RESOLVED] need formula - unique combination

    A number is generated by adding any combination of a,b,c,d, or e where a = 1, b = 2, c = 4, d = 8, e = 16.

    What I want to determine from the number is which of the combinations was used.

    Is there a simple way to do this with a formula or a loop (instead of a select case for every possible result)?

    I know if it's odd then it must include "a" but I can't get past that step.

    I have made a spreadsheet of possible values:

    VB Code:
    1. a       b       c       d        e      value
    2.  
    3. 1                   1
    4.     2               2
    5. 1   2               3
    6.         4           4
    7. 1       4           5
    8.     2   4           6
    9. 1   2   4           7
    10.             8       8
    11. 1           8       9
    12.     2       8       10
    13. 1   2       8       11
    14.         4   8       12
    15. 1       4   8       13
    16.     2   4   8       14
    17. 1   2   4   8       15
    18.                 16  16
    19. 1               16  17
    20.     2           16  18
    21. 1   2           16  19
    22.         4       16  20
    23. 1       4       16  21
    24.     2   4       16  22
    25. 1   2   4       16  23
    26.             8   16  24
    27. 1           8   16  25
    28.     2       8   16  26
    29. 1   2       8   16  27
    30.         4   8   16  28
    31. 1       4   8   16  29
    32.     2   4   8   16  30
    33. 1   2   4   8   16  31


    Thanks for your help!
    Last edited by billwagnon; Nov 5th, 2002 at 01:58 PM.

  2. #2
    Addicted Member
    Join Date
    Feb 2001
    Posts
    198
    The simplest way, rather depends on what you want to do with the information once you have 'decoded' it.

    You probably want a bitwise comparison like:

    If (combination AND 1) then (numberIncludes1)
    If (combination AND 2) =2 then (numberincludes2)
    If (combination AND 4) =4 then (numberincludes4)
    If (combination AND 8) =8 then (numberincludes8)
    If (combination AND 16) =16 then (numberincludes16)

  3. #3

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    I wanted to generate a string -- "a" or "ab" or "ace" etc.

    Then I would check the string to see if "a" is in it.

    But your way looks interesting and would give me the same effect.

    Can you explain the bitwise comparison a little more? What would this look like in vbcode?

  4. #4
    Addicted Member
    Join Date
    Feb 2001
    Posts
    198
    if combination is your number eg combination=9
    then the rest is nearly vbcode

    (Combination AND 1) will return 1 (as the 1 or lowest bii is set)

    (Combination AND 2) will return 0 (as the 2nd bit is not set)

    (Combination AND 8) will return 8 (as the 4th bit is set)

    So just replace my (numberIncludesx) with whatever routine you want to call. such as result$=result$ & "a"


    If you view the combination number as a binary number it will probably be quite obvious, 9 (decimal) is 1001 in binary.

    AND returns the result of a bitwise comparison between two numbers so(in binary): 1001 AND 1000 (8 decimal) gives the result 1000, only the 4th bit was set in both numbers.

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Here's an example.

    VB Code:
    1. Dim boo As Boolean
    2.     Dim num As Integer, mask As Integer
    3.     num = 8
    4.     mask = 4
    5.     boo = num And mask

    boo will be true or false according to whether the tested bit is 1 or 0.

    Try giving different values to the number and the mask. Here I'm testing the third bit to the right of num. For the first bit, mask=1, for the second mask =2, etc.

  6. #6

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290

    Thumbs up

    wow! that is cool!

    I don't understand how it works (yet) but it appears to be perfect!

  7. #7

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    Here's my code -

    form has a text box and textbox array (txtS). Where can I find out more about "and"??? VBHelp won't search for 'and' and I only get a one-sentence answer for 'bitwise'.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. GetSprod (Form1.Text1)
    6.  
    7. End Sub
    8.  
    9. Private Sub GetSprod(ByVal i As Integer)
    10.  
    11. Dim j As Integer
    12.  
    13. For j = 0 To txtS.Count - 1
    14.     txtS(j).Text = ""
    15. Next j
    16.  
    17. If (i And 1) = 1 Then txtS(0) = "a"
    18. If (i And 2) = 2 Then txtS(1) = "b"
    19. If (i And 4) = 4 Then txtS(2) = "c"
    20. If (i And 8) = 8 Then txtS(3) = "d"
    21. If (i And 16) = 16 Then txtS(4) = "e"
    22.  
    23.  
    24. End Sub

  8. #8
    Junior Member
    Join Date
    Sep 2002
    Posts
    19
    Divide it by 16 to get the number of that character in it. then do a mod 16 on it to grab the remainder. Divide the remainder by 8. Then mod by 8. Divide by 4, then mod by 4 and so on. It is really a simple problem.

  9. #9
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Starman
    if combination is your number eg combination=9
    then the rest is nearly vbcode

    (Combination AND 1) will return 1 (as the 1 or lowest bii is set)

    (Combination AND 2) will return 0 (as the 2nd bit is not set)

    (Combination AND 8) will return 8 (as the 4th bit is set)

    So just replace my (numberIncludesx) with whatever routine you want to call. such as result$=result$ & "a"


    If you view the combination number as a binary number it will probably be quite obvious, 9 (decimal) is 1001 in binary.

    AND returns the result of a bitwise comparison between two numbers so(in binary): 1001 AND 1000 (8 decimal) gives the result 1000, only the 4th bit was set in both numbers.
    how does this whole bitwise comparison work? (not in this example, I'm talking in general). I still don't know excalty what it means. Can you explain it a little more?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by billwagnon
    wow! that is cool!

    I don't understand how it works (yet) but it appears to be perfect!
    If b1 and b2 are two bits, then

    (b1 And b2) = 1 if both bits are 1 and 0 in any other case (1-0, 0-1 and 0-0).

    A bitwise comparison of, say, two bytes is performed in this fashion for every bit individually so, for instance:

    11000110 And
    01101011

    yields:

    01000010

  11. #11
    Addicted Member
    Join Date
    Feb 2001
    Posts
    198
    There does seem to be a surprising lack of information about logical operators, AND, OR, XOR are a few, not sure if any others are suported in VB.
    I couldn't find anything worth reading in the VB documentation, and a google search turned up this:
    http://www.logicalexpressions.com/vbtip02.htm

    Which seems to overcomplicate things for the amount of information it contains, you may find something of use in it though.

    It is pretty simple really and very much as krtxmrtz has explained.

    Take any number and convert it to binary:

    5 in binary comes out as: 0000 0101

    (that is, if you count from the right: The 1 bit is set so add 1, and the 4 bit is set so add 4, 4 + 1 = 5.)

    (Just to add confusion, there are 8 bits (in an 8 bit byte), called bit 0, bit 1, bit 2, bit 3, bit 4 etc. but sometimes it's easier to refer to them by the value that they represent if set. So bit 0 becomes the 1's bit, bit1 becomes the 2 bit, bit 2 becomes the 4 bit, bit 3 becomes the 8 bit - I tend to jump from one naming convention to another but I hope you can follow what I'm saying.)



    7 in binary comes out as 0000 0111, bits 1, 2 and 4 are set so adding these values you get 7.

    If you use the + operator which you are very familiar with:

    0000 0101 + 0000 0111 = 0000 1100 which is 12,

    If you use the AND operator, it compares the bits one at a time

    0000 0101 AND 0000 0111 = 0000 0101 (5)

    bit 0 of each was set so the result of AND is 1
    bit 1 of 7 is set, bit 1 of 5 is not set so the result (1 AND 0) is 0
    bit 2 of each number is set so the result of (1 AND 1) is 1
    all the other bits in both numbers are 0 so the result of AND for all of these is 0.

    Transfer these results to the answer and we get:
    bit 0 is set (or 1)
    bit1 not set (or 0)
    bit 3 is set (or 1)
    bits 4 to 7 are all 0
    So the result is 0000 0101

    The OR operator works similarly(on bits) but with different results, it returns a 1 if either of the two bits it was comparing (in the two numbers) is 1 or if both the bits are 1.

    7 OR 5 = 7

    The XOR operator is much more fun (if you like encryption), it is known as 'exclusive or', and returns a 1 if only one of the bits is set, but not both.

    7 XOR 5 = 2 as only the two's bit (bit1) was different between the two numbers.

    hope this is of some use.

  12. #12

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    this site is useful:

    http://www.learnbinary.com/

  13. #13

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    questions:

    is a 64-bit architecture one where all numbers are expressed as 64 zeros or ones?

    how do you designate a decimal point - is that an arbitrary choice made by the OS designer?

    thanks for any light!

  14. #14
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by billwagnon
    questions:

    how do you designate a decimal point - is that an arbitrary choice made by the OS designer?

    thanks for any light!
    About your second question, I'm not sure if that's what you want, but you may want to take a look at http://www.nuvisionmiami.com/books/a...oating_tut.htm

    Also, you may try the howstuffworks pages, particularly this one:

    http://fitness.howstuffworks.com/bytes6.htm

    Good luck.

  15. #15

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    okay I learned something today

    can I go home now?

  16. #16
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    No
    Not at all related to sheep...

  17. #17

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    you jolly well waited 'til the end of the day to tell me!

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