{"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":9,"footnotes":""},"categories":[7],"tags":[29,4840,265,76,477],"class_list":["post-3666","post","type-post","status-publish","format-standard","hentry","category-grails","tag-gorm","tag-grails","tag-hibernate","tag-mysql","tag-optimization"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Mohd Farid\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"A use case of Bitwise AND | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"A use case of Bitwise AND | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#article\",\"name\":\"A use case of Bitwise AND | TO THE NEW Blog\",\"headline\":\"A use case of Bitwise AND\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/farid\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2011-04-15T00:50:12+05:30\",\"dateModified\":\"2011-04-15T11:34:27+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":5,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#webpage\"},\"articleSection\":\"Grails, GORM, Grails, hibernate, MySql, optimization\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#listItem\",\"name\":\"A use case of Bitwise AND\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#listItem\",\"position\":3,\"name\":\"A use case of Bitwise AND\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/farid\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/farid\\\/\",\"name\":\"Mohd Farid\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d386ff6714165be4c5ee1f75fd255d6ca7021e0ada16952088cff7e9f6e50679?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Mohd Farid\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/\",\"name\":\"A use case of Bitwise AND | TO THE NEW Blog\",\"description\":\"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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/a-use-case-of-bitwise-and\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/farid\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/farid\\\/#author\"},\"datePublished\":\"2011-04-15T00:50:12+05:30\",\"dateModified\":\"2011-04-15T11:34:27+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"A use case of Bitwise AND | TO THE NEW Blog","description":"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","canonical_url":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#article","name":"A use case of Bitwise AND | TO THE NEW Blog","headline":"A use case of Bitwise AND","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/farid\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2011-04-15T00:50:12+05:30","dateModified":"2011-04-15T11:34:27+05:30","inLanguage":"en-US","commentCount":5,"mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#webpage"},"articleSection":"Grails, GORM, Grails, hibernate, MySql, optimization"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/www.tothenew.com\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#listItem","name":"A use case of Bitwise AND"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#listItem","position":3,"name":"A use case of Bitwise AND","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/farid\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/farid\/","name":"Mohd Farid","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d386ff6714165be4c5ee1f75fd255d6ca7021e0ada16952088cff7e9f6e50679?s=96&d=mm&r=g","width":96,"height":96,"caption":"Mohd Farid"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/","name":"A use case of Bitwise AND | TO THE NEW Blog","description":"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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/farid\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/farid\/#author"},"datePublished":"2011-04-15T00:50:12+05:30","dateModified":"2011-04-15T11:34:27+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"A use case of Bitwise AND | TO THE NEW Blog","og:description":"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","og:url":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"A use case of Bitwise AND | TO THE NEW Blog","twitter:description":"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","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"3666","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:14:01","updated":"2024-02-29 10:59:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tA use case of Bitwise AND\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Grails","link":"https:\/\/www.tothenew.com\/blog\/category\/grails\/"},{"label":"A use case of Bitwise AND","link":"https:\/\/www.tothenew.com\/blog\/a-use-case-of-bitwise-and\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3666","targetHints":{"allow":["GET"]}}],"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}]}}