{"id":3666,"date":"2011-04-15T00:50:12","date_gmt":"2011-04-14T19:20:12","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3666"},"modified":"2011-04-15T11:34:27","modified_gmt":"2011-04-15T06:04:27","slug":"a-use-case-of-bitwise-and","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/","title":{"rendered":"A use case of Bitwise AND"},"content":{"rendered":"<p>Recently, I used bitwise Anding in my grails project. I am sharing the approach that we followed by means of an example.<br \/>\n<\/br><br \/>\nSuppose we have a domain class Person which can have multiple attributes like Smart, Intelligent, Talkative etc.<\/p>\n<p>What sort of relationship comes to our mind when we see this?<br \/>\nI believe the obvious answer would be Person hasMany Attributes<\/p>\n<p>Next, what if we want to optimize our design so that the searches on the Person becomes better.<\/p>\n<p>Well, this was the question we were facing in our recent project where we finally used the bitwise AND.<br \/>\nWe decided to have one field of type Long in our Person class. We named this as attributes . Soon, I ll be explaining  why a long for Attribute&#8230;<\/p>\n<p>We ensured that the Attributes are never going to be more than 20-30 . So, it became a nice candidate which could utilize the benefit of Bitwise ANDing.<\/p>\n<p>We associated a binary weight to every instance of Attribute domain class.<\/p>\n<p>The rows in the attribute table of our database looked somewhat like this<br \/>\n[java]<br \/>\nid | attribute      | weight<br \/>\n1 | Smart          | 1<br \/>\n2 | Talkative      | 2<br \/>\n3 | Intelligent     | 4<br \/>\n4 | Cooperative  | 8<br \/>\n5 | Ignorant       | 16<br \/>\n[\/java]<br \/>\nSo, in order to save a Person who is Smart and Cooperative , we would set his attributes field.<br \/>\nweight of Smart + weight of Cooperative.<br \/>\nie 1+8 = 9.<\/p>\n<p>Suppose our Person table has some entries as follows:<\/p>\n<p>[java]<br \/>\nid  | name  | attributes<br \/>\n1   | Tom   |  9<br \/>\n2   | Fred   |  12<br \/>\n3   | John   | 18<br \/>\n[\/java]<\/p>\n<p>If we want to search a person who is Intelligent  we need to have a final query like this<br \/>\n[java]<br \/>\nSelect * from person where attributes&amp;4 = 4<br \/>\n(4 is the weight of Attribute &#8211; Intelligent)<br \/>\n[\/java]<br \/>\nSearch a person who is Cooperative and Talkative<br \/>\n[java]<br \/>\nSelect * from person where attributes&amp;2=2 and attributes&amp;8=8<br \/>\nor more optimally,<br \/>\nSelect * from person where attributes&amp;10=10<br \/>\n(2 is the weight of Talkative and 8 is the weight of Cooperative)<br \/>\n[\/java]<\/p>\n<p><strong><br \/>\nSome points to be kept in mind before taking this approach:<\/strong><br \/>\nThere is a maximum limit of 64 values for the field on which we plan to do BitwiseANDing.<br \/>\nBitwise AND is not directly supported by Hibernate, but we can add a custom function and dialect.<\/p>\n<p><\/br><br \/>\nHope this helped.<\/p>\n<p>Thanks &amp; Regards<br \/>\nMohd Farid<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I used bitwise Anding in my grails project. I am sharing the approach that we followed by means of an example. Suppose we have a domain class Person which can have multiple attributes like Smart, Intelligent, Talkative etc. What sort of relationship comes to our mind when we see this? I believe the obvious [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[29,4840,265,76,477],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3666"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3666"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3666\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}