Using bitwise operations to filter results based on user selected filters in javascript





5.00/5 (2 votes)
Not sure I got it correctly... But if I did, here's a simpler solution which doesn't rely on implication. Bitfields are ubiquitous in C programming, they are used to specify flags. If, for instance, your hotel requirements amount to bitfield 01101010 then you can simply AND the bitfield with...
Not sure I got it correctly... But if I did, here's a simpler solution which doesn't rely on implication. Bitfields are ubiquitous in C programming, they are used to specify flags. If, for instance, your hotel requirements amount to bitfield
01101010
then you can simply AND
the bitfield with each candidate hotel and require that the result equals your bitfield. So for instance, if a candidate hotel offers services 11111010
then 01101010 & 11111010 == 01101010
so it's returned as a result. Note that the candidate hotel actually offers more services than those requested, but includes all of our requirements. If a different candidate hotel offers 01111010
then 10111010 & 01101010 == 00101010 != 01101010
so this hotel is not returned with the results because it fails to provide requirement #6 (zero based).