Question

Researching the possibility of using bitwise comparison to assess what options have been selected out a possible 100 options.

now as an integer the selection of all options would require storage of an integer of 2 to the power of 99 (6E29). way beyond the limit of circa 9E18.

just as with dir permissions ( 1 = read, 2= write, 4 = execute) 1+2+4 = 7 = full access.

I would like to know which of the 100 options have been chosen by the same method.

Any advice/tips much appreciated.

NB storage will be mysql

-- EDIT --

The end goal here is to simplify a check as to what currencies a user can be paid in.

assigning values to currency like so:

Currency OptVal
GBP      1
USD      2
EUR      4
AUD      8
CAD      16
ZAR      32

and so on (there are many many currencies and more will arise through crypto currencies I'm sure)

it would then be convenient to check which currencies a user has using bitwise operators...

so if a user had currency setting of 3 only GBP and USD. 5 GBP & EUR 63 GBP,USD,EUR,AUD,CAD,ZAR

and so on - hope this clarifies the goal.

The issue is to do this in its most simplistic form of storing that integer when you have > 100 currencies. you need a value 2E(n-1) for each option and for large n this number is very large and not storable as an integer (BIGINT Max value is 18446744073709551615)

Was it helpful?

Solution

You want advice. Don't do it this way.

MySQL offers the boolean data type, which is convenient for flags. Each value does occupy one byte, so the storage will be larger than using bits.

MySQL also offers the bit() data type, where you can put together up to 64 bits. You can read about them here.

Using built-in data types is simply the right way to go. They protect your from changes of server, from upgrades on the OS, and the possibility that your application and server have different endian-ness (if you don't know what this is, then you definitely should not be thinking about bit fiddling).

The good news is that there are data types for what you want to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top