|
-
Nov 5th, 2002, 11:44 AM
#1
Thread Starter
Hyperactive Member
[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:
a b c d e value
1 1
2 2
1 2 3
4 4
1 4 5
2 4 6
1 2 4 7
8 8
1 8 9
2 8 10
1 2 8 11
4 8 12
1 4 8 13
2 4 8 14
1 2 4 8 15
16 16
1 16 17
2 16 18
1 2 16 19
4 16 20
1 4 16 21
2 4 16 22
1 2 4 16 23
8 16 24
1 8 16 25
2 8 16 26
1 2 8 16 27
4 8 16 28
1 4 8 16 29
2 4 8 16 30
1 2 4 8 16 31
Thanks for your help!
Last edited by billwagnon; Nov 5th, 2002 at 01:58 PM.
-
Nov 5th, 2002, 12:23 PM
#2
Addicted Member
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)
-
Nov 5th, 2002, 12:27 PM
#3
Thread Starter
Hyperactive Member
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?
-
Nov 5th, 2002, 01:20 PM
#4
Addicted Member
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.
-
Nov 5th, 2002, 01:52 PM
#5
Here's an example.
VB Code:
Dim boo As Boolean
Dim num As Integer, mask As Integer
num = 8
mask = 4
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.
-
Nov 5th, 2002, 01:58 PM
#6
Thread Starter
Hyperactive Member
wow! that is cool!
I don't understand how it works (yet) but it appears to be perfect!
-
Nov 5th, 2002, 02:51 PM
#7
Thread Starter
Hyperactive Member
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:
Option Explicit
Private Sub Command1_Click()
GetSprod (Form1.Text1)
End Sub
Private Sub GetSprod(ByVal i As Integer)
Dim j As Integer
For j = 0 To txtS.Count - 1
txtS(j).Text = ""
Next j
If (i And 1) = 1 Then txtS(0) = "a"
If (i And 2) = 2 Then txtS(1) = "b"
If (i And 4) = 4 Then txtS(2) = "c"
If (i And 8) = 8 Then txtS(3) = "d"
If (i And 16) = 16 Then txtS(4) = "e"
End Sub
-
Nov 6th, 2002, 12:56 AM
#8
Junior Member
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.
-
Nov 6th, 2002, 02:44 AM
#9
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!!
-
Nov 6th, 2002, 03:20 AM
#10
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
-
Nov 6th, 2002, 05:38 AM
#11
Addicted Member
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.
-
Nov 6th, 2002, 09:35 AM
#12
Thread Starter
Hyperactive Member
-
Nov 6th, 2002, 09:37 AM
#13
Thread Starter
Hyperactive Member
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!
-
Nov 6th, 2002, 10:03 AM
#14
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.
-
Nov 6th, 2002, 10:09 AM
#15
Thread Starter
Hyperactive Member
okay I learned something today
can I go home now?
-
Nov 6th, 2002, 04:54 PM
#16
Addicted Member
No
Not at all related to sheep...
-
Nov 6th, 2002, 04:54 PM
#17
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|