Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need algorithm
Message
From
07/07/2011 10:25:27
 
General information
Forum:
Games
Category:
Trivia
Title:
Miscellaneous
Thread ID:
01517506
Message ID:
01517581
Views:
40
>>>>>>>>I need to create algorithm (based on an integer number) that by applying some formula or equation to this number I can identify if the number belongs to group 1, 2, or 3 or any combination of them (e.g. 1 and 2, 1 and 3, 1 and 2 and 3, 2 and 3)
>>>>>>>>For example, number 8 would indicate belonging to all three groups (since I can divide this number by the group number without a remainder.
>>>>>>>>Any suggestions?
>>>>>>>
>>>>>>>It just occured to me that the answer is very simple.
>>>>>>
>>>>>>
>>>>>>Is this about your options ? (from yesterday)
>>>>>>
>>>>>>Then the groups are just bits. Test witth BitTest()
>>>>>>
>>>>>>And belonging to all three groups = 7
>>>>>
>>>>>Yes, this is a follow up to the options thread from yesterday. But I can't seem to grasp how to apply BitTest() to this issue. I can't understand how the value of 7 by using BitTest() indicates that 7 belongs to all three groups? Could you please explain?
>>>>
>>>>
>>>>option1 is 2^0 , ie 0 or 1
>>>>option2 is 2^1 , ie 0 or 2
>>>>option3 is 2^2 , ie 0 or 4
>>>>
>>>>
>>>>So, if all options are set, you get 4+2+1 = 7
>>>>
>>>>
>>>>n = 7
>>>>
>>>>hasOption1 = bittest(n, 0)  && is bit 0 set  ?
>>>>
>>>>hasOption2 = bittest(n, 1) && is bit 1 set ?
>>>>
>>>>hasOption3 = bittest(n, 2) && is bit 2 set ?
>>>>
>>>>
>>>>
>>>>If n is 5 ( 1+4) then it's option1 and option3
>>>
>>>Thank you. I think between the explanation you kindly provided and the articles Naomi provided I will finally get it.
>>
>>
>>It's just the sum of some powers of 2. Each bit represents a power of 2
>>
>>On a 32 bit system:
>>
>>The sum of 2^n, for n = 0 to 31, is 2^32 -1
>>
>>In your case
>>
>>3 options, going from 0 to 2
>>
>>The sum of 2^n, for n = 0 to 2, is 2^3 - 1 (8-1 = 7) . In total 2^3 possibilities
>
>What I don't understand is how do you go from bit to number that will be stored in the field. For example, say only the 1st option box is selected. Does it mean that the value of 1 is set to the field? And if Option 1 and 2 are selected, the value of 3 is set to the field? And in case of all three options boxes, the value is 7?

Yes. You have to assign a (different) bit to each choice. Since bits are numbered from right to left starting with 0, it seems natural to use bit 0 for choice1, bit 1 for choice2, bit2 for choice3.

Choice 1 is bit 0. If it is set then the value will be 2^0 or 1
Choice 2 is bit 1. If it is set then the value will be 2^1 or 2
Choice 3 is bit 2. If it is set then the value will be 2^2 or 4

So the value is 2^(bit number)




>(Do you think that the reason for stupid questions is that I drank too much beer when I was young or not enoug? <g>)
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform