Jump to content

How do you make unique option codes for input as a parameter?


seeker0003

Recommended Posts

I'm unsure of what the name for the method I'm looking for is, making it difficult to search for.

There is a method of assigning multiple options a number code, and the end user can add together the number codes of their desired choices, and then inputs the result, which is unique for every potential option combination.

What I'm wondering is, would someone please tell me what the name of this method is? And maybe how to generate such codes, or a better option. I'm working on a game mod that currently requires text-parsing for option setting, and I need to enable around 12 options in the limited entry space, preferably with greatest user ease. Thanks.

Link to comment
Share on other sites

It sounds like you want to encode the options as bits by making every option a value a power of 2 (A = 1, B = 2, C = 4, etc.) Then when you add the selected options together, it is equivalent to setting the appropriate bits. You can test any option with a boolean AND; for example: (choices & B) > 0 will be true if option B is in the choices.

 

I don't know if this technique has a name or not ...

Link to comment
Share on other sites

Bitwise operation

https://en.wikipedia.org/wiki/Bitwise_operation

 

To set bit, you should use

flags |= 1 << x

 

To clear bit, you should use

flags &= ~( 1 << x );

 

and to test bit you should use

if( flags & ( 1 << x ) )

{

// bit set

}

else

{

// bit clear

}

 

where x is bit index from 0...31 for 32 bit integer.

<< is shift operator.

https://en.wikipedia.org/wiki/Arithmetic_shift

Edited by Sensei
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.