I am trying to solve a problem that involves basically implementing a logical AND between the input parameter.
The complexity of the problem involves the size of the input parameters. To give a high level overview, I am trying to implement the logic similar to
100 & 100 == 100
001 & 010 == 0
001 & 100 == 0
.....
The complexity is that some of the input parameters can be 400 bits long. Its not a true binary number representation. It’s more of a positional representation. The same input can be represented as
100 = x1; (or) x100
011 = x2,3; (or) x011
001.......11 = x3,......450,451;
So basically “x” is just a prefix with the value for it. This is an ACL system designed a long time ago and I am trying to implement a Java version for it.
I couldn’t find a data type in Java that could be used to represent a binary representation that is as huge as having 400 bits. I can also use the decimal representation [ie., x2,3] and solve it too, but I couldn’t think of way other than looping through the entire number range and comparing it with the other input parameter. Both input parameters could be normalized to the same representation format [ie., binary or decimal].
Any suggestions (or) help on how I can solve this problem?
You could use a
BitSet. It has support for bitwise and-operations and should handle 400 bits quite well.Here is an example:
To parse a
x110101string, you could do something likeIf you still don’t like that approach, you could use a
Set<Integer>containing the indecies of the ones. To figure out the “and” between two such sets, you simply doset1.retainAll(set2).Here is an example: