{"id":93,"date":"2006-10-25T18:00:52","date_gmt":"2006-10-25T22:00:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=93"},"modified":"2019-10-22T16:31:15","modified_gmt":"2019-10-22T20:31:15","slug":"bit-slices","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/10\/25\/bit-slices\/","title":{"rendered":"Bit slices"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Blog reader Kathirvel <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=87#comment-5408\">wanted to know more<\/a> about the MATLAB default image and how it was formed.\r\n   <\/p>\r\n   <p>I'll show briefly how it was done, using a small magic square and a Pascal matrix.<\/p>\r\n\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">m = magic(3)<\/pre><pre style=\"font-style:oblique\">\r\nm =\r\n\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">p = pascal(3)<\/pre><pre style=\"font-style:oblique\">\r\np =\r\n\r\n     1     1     1\r\n     1     2     3\r\n     1     3     6\r\n\r\n<\/pre><p>The values of these two matrices can be stored in a single matrix by using <tt>bitshift<\/tt> and <tt>bitor<\/tt>.  Let's let the magic square occupy the least-significant four bits, and let the Pascal matrix occupy the next four.  We\r\n      need to bit-shift the values in the Pascal matrix by four bits, and then bit-or the result with the magic square.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">p_shifted = bitshift(p, 4)<\/pre><pre style=\"font-style:oblique\">\r\np_shifted =\r\n\r\n    16    16    16\r\n    16    32    48\r\n    16    48    96\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">combined = bitor(m, p_shifted)<\/pre><pre style=\"font-style:oblique\">\r\ncombined =\r\n\r\n    24    17    22\r\n    19    37    55\r\n    20    57    98\r\n\r\n<\/pre><p>Look at the 8-bit binary representation of <tt>combined(3,3)<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dec2bin(combined(3,3), 8)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n01100010\r\n\r\n<\/pre><p>Compare that with the 4-bit binary representation of <tt>m(3,3)<\/tt> and <tt>p(3,3)<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dec2bin(m(3,3), 4)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n0010\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dec2bin(p(3,3), 4)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n0110\r\n\r\n<\/pre><p>You can see how <tt>combined(3,3)<\/tt> contains the bits from both <tt>m(3,3)<\/tt> and <tt>p(3,3)<\/tt>.  The original values can be recovered from <tt>combined<\/tt> by using <tt>bitshift<\/tt> and <tt>bitand<\/tt>.\r\n   <\/p>\r\n   <p>Check out Loren's <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=60\">Art of MATLAB post today<\/a> - it has more information about using MATLAB bit functions.\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_f9ba64fc49a44987b151227c6dea7794() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f9ba64fc49a44987b151227c6dea7794 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f9ba64fc49a44987b151227c6dea7794';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n-->\r\n      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f9ba64fc49a44987b151227c6dea7794()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\nf9ba64fc49a44987b151227c6dea7794 ##### SOURCE BEGIN #####\r\n%% Bit slices\r\n% Blog reader Kathirvel <https:\/\/blogs.mathworks.com\/steve\/?p=87#comment-5408 \r\n% wanted to know more> about the MATLAB default image\r\n% and how it was formed.\r\n%\r\n% I'll show briefly how it was done, using a small magic square and a \r\n% Pascal matrix.\r\n\r\nm = magic(3)\r\n\r\n%%\r\n\r\np = pascal(3)\r\n\r\n%%\r\n% The values of these two matrices can be stored in a single matrix by\r\n% using <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/index.html?\/access\/helpdesk\/help\/techdoc\/ref\/bitshift.html\r\n% |bitshift|> and \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/index.html?\/access\/helpdesk\/help\/techdoc\/ref\/bitor.html\r\n% |bitor|>.  Let's let the magic square occupy the\r\n% least-significant four bits, and let the Pascal matrix occupy the next\r\n% four.  We need to bit-shift the values in the Pascal matrix by four bits,\r\n% and then bit-or the result with the magic square.\r\n\r\np_shifted = bitshift(p, 4)\r\n\r\n%%\r\n\r\ncombined = bitor(m, p_shifted)\r\n\r\n%%\r\n% Look at the 8-bit binary representation of |combined(3,3)|:\r\n\r\ndec2bin(combined(3,3), 8)\r\n\r\n%%\r\n% Compare that with the 4-bit binary representation of |m(3,3)| and\r\n% |p(3,3)|:\r\n\r\ndec2bin(m(3,3), 4)\r\n\r\n%%\r\n\r\ndec2bin(p(3,3), 4)\r\n\r\n%%\r\n% You can see how |combined(3,3)| contains the bits from both |m(3,3)| and\r\n% |p(3,3)|.  The original values can be recovered from |combined| by using\r\n% |bitshift| and \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/index.html?\/access\/helpdesk\/help\/techdoc\/ref\/bitand.html \r\n% |bitand|>.\r\n%\r\n% Check out Loren's <https:\/\/blogs.mathworks.com\/loren\/?p=60 Art of MATLAB \r\n% post today> - it has more information about using MATLAB bit functions.\r\n\r\n##### SOURCE END ##### f9ba64fc49a44987b151227c6dea7794\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Blog reader Kathirvel wanted to know more about the MATLAB default image and how it was formed.\r\n   \r\n   I'll show briefly how it was done, using a small magic square and a Pascal matrix.\r\n\r\nm =...<p>Blog reader Kathirvel <a href=\"http:\/\/blogs.mathworks.com\/steve\/?p=87#comment-5408\">wanted to know more<\/a> about the MATLAB default image and how it was formed.\r\n   <\/p>\r\n   <p>I'll show briefly how it was done, using a small magic square and a Pascal matrix.<\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[260,258,262,54,256],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/93"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":2698,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/93\/revisions\/2698"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}