{"id":23953,"date":"2019-06-04T04:44:42","date_gmt":"2019-06-04T04:44:42","guid":{"rendered":"https:\/\/www.bridge-global.com\/blog\/?p=23953"},"modified":"2019-07-10T02:38:18","modified_gmt":"2019-07-10T02:38:18","slug":"the-magic-of-a-single-integer-number-to-contain-huge-information","status":"publish","type":"post","link":"https:\/\/www.bridge-global.com\/blog\/the-magic-of-a-single-integer-number-to-contain-huge-information\/","title":{"rendered":"The Magic of a Single Integer Number to Contain Huge Information"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Here I am trying to explain how we can effectively store huge pieces of information by consuming less space. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">For this, I am taking an example of <\/span><b>\u201cHow to store all roles of a user in a single integer value.\u201d <\/b><\/p>\n<p><span style=\"font-weight: 400;\">When developing application software, a programmer should write a module for handling Roles and Permissions for the users. Suppose we have the following roles.<\/span><span style=\"font-weight: 400;\">\f<\/span><\/p>\n<table style=\"border-style: solid; border-color: #cccccc; height: 216px; width: 186px; padding: 5px;\" border=\"1\" width=\"186\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><b>Role<\/b><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span class=\"test\" style=\"font-weight: 400;\"> Anonymous<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">Admin<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">SuperAdmin<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">General Manager<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\"> Senior Manager<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">Data Entry<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">View Only<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; width: 176px; height: 24px;\"><span style=\"font-weight: 400;\">ReportsOnly<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">\u00a0The programmer will usually assign an integer ID to each role like the following:<\/span><\/p>\n<table style=\"border-style: solid; border-color: #cccccc; height: 313px; width: 186px; padding: 5px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><b>Id<\/b><\/td>\n<td style=\"padding: 5px; height: 24px;\"><b>Role<\/b><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\"> Anonymous<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">Admin<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">SuperAdmin<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">General Manager<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">Senior Manager<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">Data Entry<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">7<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">View Only<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td style=\"padding: 5px; height: 24px;\"><span style=\"font-weight: 400;\">ReportsOnly<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Suppose the user with ID 1 has the roles of <\/span><b>Data Entry<\/b><span style=\"font-weight: 400;\"> and <\/span><b>View Only. <\/b><\/p>\n<p><span style=\"font-weight: 400;\">In this case, the programmer can save user permissions in two different ways:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a string with roles separated by a comma, for example, <\/span><b>6,7 <\/b><span style=\"font-weight: 400;\">and this can be stored in the column. While retrieving back, we can split the string by comma and find the role IDs. (The problem with this approach is that the permission data will end up with a big string)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Each role IDs can be stored against user id by consuming each column for each role ID as follows: <\/span><\/li>\n<\/ol>\n<table style=\"height: 95px; border-style: solid; width: 174px; border-color: #cccccc; padding: 5px;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"padding: 5px; width: 77px;\"><b>User ID<\/b><\/td>\n<td style=\"padding: 5px; width: 77px;\"><b>Role ID<\/b><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 77px;\"><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td style=\"padding: 5px; width: 77px;\"><span style=\"font-weight: 400;\">6<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 77px;\"><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td style=\"padding: 5px; width: 77px;\"><span style=\"font-weight: 400;\">7<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">In this case, we need a separate table for storing information on user roles.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But there is another better way we can store any combination of user roles as a single integer value. For this, the Role ID should be a number which is the power of 2.<\/span><\/p>\n<table style=\"height: 387px; border-style: solid; width: 299px; border-color: #cccccc; padding: 5px;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>Id<\/b><\/td>\n<td style=\"padding: 5px; width: 193px;\"><b>Role<\/b><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>2<\/b><sup><b>0<\/b><\/sup><span style=\"font-weight: 400;\"> = 1<\/span><\/td>\n<td style=\"padding: 5px; width: 193px;\"><span style=\"font-weight: 400;\"> Anonymous<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>2<\/b><sup><b>1 <\/b><\/sup><span style=\"font-weight: 400;\">= 2<\/span><\/td>\n<td style=\"padding: 5px; width: 193px;\"><span style=\"font-weight: 400;\">Admin<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>2<\/b><sup><b>2<\/b><\/sup><span style=\"font-weight: 400;\"> = 4<\/span><\/td>\n<td style=\"padding: 5px; width: 193px;\"><span style=\"font-weight: 400;\">SuperAdmin<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>2<\/b><sup><b>3 <\/b><\/sup><span style=\"font-weight: 400;\">= 8<\/span><\/td>\n<td style=\"padding: 5px; width: 193px;\"><span style=\"font-weight: 400;\">General Manager<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px;\"><b>2<\/b><sup><b>4 <\/b><\/sup><span style=\"font-weight: 400;\">= 16<\/span><\/td>\n<td style=\"padding: 5px; width: 193px;\"><span style=\"font-weight: 400;\">Senior Manager<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px; background-color: #f7f4d2;\"><b>2<\/b><sup><b>5 <\/b><\/sup><span style=\"font-weight: 400;\">= 32<\/span><\/td>\n<td style=\"padding: 5px; width: 193px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">Data Entry<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 86px; background-color: #f7f4d2;\"><b>2<\/b><sup><b>6 <\/b><\/sup><span style=\"font-weight: 400;\">= 64<\/span><\/td>\n<td style=\"padding: 5px; width: 193px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">View Only<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">In this case, the user with Id 1 has the roles of <\/span><b>Data Entry<\/b><span style=\"font-weight: 400;\"> and <\/span><b>View Only<\/b><span style=\"font-weight: 400;\"> can be stored as 32 + 64 =<\/span><b> 96<\/b><span style=\"font-weight: 400;\">. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">In one single integer column, we can store the user roles combination. Here we can store <\/span><b>96 <\/b><span style=\"font-weight: 400;\">as user permission.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Well, We have stored the user roles information, Now we need to extract the roles from the single integer number. Let find out how we can do this.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We need a reverse algorithm to do this.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, user permission is <\/span><b>96<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Divide the number with 2 and record the remainder of it,<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Divide the answer again and again until you get answer zero<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Take the iteration number of all items in which that the remainder is 1<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Take the power 2 of that number<\/span><\/li>\n<\/ul>\n<p><b>Eg:<\/b><\/p>\n<table style=\"height: 326px; border-style: solid; width: 381px; border-color: #cccccc; padding: 5px;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><b>Iteration <\/b><\/td>\n<td style=\"padding: 5px; width: 52px;\"><\/td>\n<td style=\"padding: 5px; width: 43px;\"><\/td>\n<td style=\"padding: 5px; width: 195px;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td style=\"padding: 5px; width: 52px;\"><span style=\"font-weight: 400;\">96<\/span><\/td>\n<td style=\"padding: 5px; width: 43px;\"><span style=\"font-weight: 400;\">96\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px;\"><span style=\"font-weight: 400;\">Answer: 48, Remainder: 0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td style=\"padding: 5px; width: 52px;\"><span style=\"font-weight: 400;\">48<\/span><\/td>\n<td style=\"padding: 5px; width: 43px;\"><span style=\"font-weight: 400;\">48\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px;\"><span style=\"font-weight: 400;\">Answer: 24, Remainder: 0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td style=\"padding: 5px; width: 52px;\"><span style=\"font-weight: 400;\">24<\/span><\/td>\n<td style=\"padding: 5px; width: 43px;\"><span style=\"font-weight: 400;\">24\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px;\"><span style=\"font-weight: 400;\">Answer: 12, Remainder: 0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td style=\"padding: 5px; width: 52px;\"><span style=\"font-weight: 400;\">12<\/span><\/td>\n<td style=\"padding: 5px; width: 43px;\"><span style=\"font-weight: 400;\">12\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px;\"><span style=\"font-weight: 400;\">Answer: 6, Remainder: 0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px;\"><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td style=\"padding: 5px; width: 52px;\"><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td style=\"padding: 5px; width: 43px;\"><span style=\"font-weight: 400;\">6\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px;\"><span style=\"font-weight: 400;\">Answer: 3, Remainder: 0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px; background-color: #f7f4d2;\"><b>6<\/b><\/td>\n<td style=\"padding: 5px; width: 52px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td style=\"padding: 5px; width: 43px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">3\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">Answer: 1, Remainder: <\/span><b>1<\/b><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 5px; width: 51px; background-color: #f7f4d2;\"><b>7<\/b><\/td>\n<td style=\"padding: 5px; width: 52px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td style=\"padding: 5px; width: 43px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">1\/2<\/span><\/td>\n<td style=\"padding: 5px; width: 195px; background-color: #f7f4d2;\"><span style=\"font-weight: 400;\">Answer: 0, Remainder: <\/span><b>1<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Now find the items which have remainder 1, we have 6th and 7th iteration. So the split items can be calculated by<\/span><\/p>\n<p><span style=\"font-weight: 400;\">6th power of 2: 2<\/span><sup><span style=\"font-weight: 400;\">6<\/span><\/sup><span style=\"font-weight: 400;\">=32<\/span><\/p>\n<p><span style=\"font-weight: 400;\">7th power of 2: 2<\/span><sup><span style=\"font-weight: 400;\">7<\/span><\/sup><span style=\"font-weight: 400;\">=64<\/span><\/p>\n<p><b><i>\f<\/i><\/b><span style=\"font-weight: 400;\">The C# code for splitting the values into corresponding power of 2 is given below<\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"><code>public static List&lt;long&gt; GetSumOfPowerOfTwo(long n)\r\n\u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 var remainders = new List&lt;int&gt;();\r\n\u00a0 \u00a0 \u00a0 \u00a0 var powerOfTwo = new List&lt;long&gt;();\r\n\u00a0 \u00a0 \u00a0 \u00a0 while (n &gt; 0)\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 remainders.Add((int)n % 2);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 n = n \/ 2;\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 for (var i = 0; i &lt; remainders.Count; i++)\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (remainders[i] == 1)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 powerOfTwo.Add((long)Math.Pow(2, i));\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 return powerOfTwo;\r\n\u00a0 \u00a0 }<\/code><\/span><\/pre>\n<p><b><i>* You can store any type of combination information in this way apart from user roles <\/i><\/b><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Here I am trying to explain how we can effectively store huge pieces of information by consuming less space. For this, I am taking an example of \u201cHow to store all roles of a user in a single integer value.\u201d &hellip;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":90,"featured_media":24144,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-23953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"featured_image_src":"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2019\/06\/Storing-Huge-Infomation-in-A-Single-Integer-Value.jpg","author_info":{"display_name":"Sukesh Chand","author_link":"https:\/\/www.bridge-global.com\/blog\/author\/sukesh-chand\/"},"_links":{"self":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/23953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/comments?post=23953"}],"version-history":[{"count":34,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/23953\/revisions"}],"predecessor-version":[{"id":25559,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/23953\/revisions\/25559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media\/24144"}],"wp:attachment":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media?parent=23953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/categories?post=23953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/tags?post=23953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}