Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Javascript
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jul 2011CPOL 9K   1   2
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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Greece Greece
I am a software developer (mainly in C# and T-SQL) for a project management company in Athens, Greece. I have been working with computers since early 1987. I am adept at Pascal, C, C++, Java (my MSc was sponsored by Sun Microsystems), Lisp, Scheme, F#, C# VB.Net, Perl and some others that are too obscure to mention. When I want a quick and dirty solution to a programming problem I use a functional language, such as Haskell, Scheme or, more recently, F#.

I also play the keyboards and compose music.

---------------------------------------------------------

MSc Distributed Systems and Networks - University of Kent at Canterbury
BEng Computer Systems Engineering - University of Kent at Canterbury

Comments and Discussions

 
GeneralReason for my vote of 5 ya good. Pin
Monjurul Habib27-Jul-11 22:17
professionalMonjurul Habib27-Jul-11 22:17 
GeneralHeh, I knew there had to be a simpler way, and who better to... Pin
yiangos26-Jul-11 22:09
professionalyiangos26-Jul-11 22:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.