{"id":3739,"date":"2020-06-29T08:24:09","date_gmt":"2020-06-29T12:24:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3739"},"modified":"2020-09-11T10:25:29","modified_gmt":"2020-09-11T14:25:29","slug":"news-on-the-hexadecimal-and-binary-numeric-fronts","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2020\/06\/29\/news-on-the-hexadecimal-and-binary-numeric-fronts\/","title":{"rendered":"News on the Hexadecimal and Binary Numeric Fronts"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>I'm sure you've all heard the joke before:<\/p><pre>  There are 10 kinds of people in the world\r\n  Those who understand binary and those who don't<\/pre><p>By the way, the correct pronunciation of the third symbol in the first line of the joke is \"two\", not \"ten\".  The hex \"equivalent\" is a bit rude so I won't post it here.<\/p><p>Well, no joke now.  We've gotten serious about helping you read or specify hex and binary values now, and covering a broader range (i.e., negative hex(!) now.  Let me tell you a bit more.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#6691a0f9-e975-4a2c-a8e4-4728637eee34\">Enter Hex and Binary Values<\/a><\/li><li><a href=\"#4f761765-394c-41ef-b3ce-b2ae00bdc935\">Represent Values as Text<\/a><\/li><li><a href=\"#3b457734-2a6f-4344-8d40-f609a2f8e75d\">Bitwise Operations with Binary Values<\/a><\/li><li><a href=\"#0322b225-b7bc-43b2-9cb4-3b64af362c51\">Summary<\/a><\/li><\/ul><\/div><h4>Enter Hex and Binary Values<a name=\"6691a0f9-e975-4a2c-a8e4-4728637eee34\"><\/a><\/h4><p>I don't work with hex and binary data that much myself, but there are plenty of people who do. We recently made some updates to how we can help you handle such data. Look <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/specify-hexadecimal-and-binary-numbers.html\">here<\/a> for the details.<\/p><p>We've always been able to do this:<\/p><pre class=\"codeinput\">A = 0.25\r\nB = 3\r\nC = 1\/pi\r\n<\/pre><pre class=\"codeoutput\">A =\r\n    0.2500\r\nB =\r\n     3\r\nC =\r\n    0.3183\r\n<\/pre><p>and these are all double values in MATLAB.<\/p><p><tt>B<\/tt> could be represented in one of the integer forms. To do so in MATLAB, you would either convert, or you might have read in values from some other file format and specified the integer type for reading in some of the bits.<\/p><pre class=\"codeinput\">Buint8 = uint8(B)\r\n<\/pre><pre class=\"codeoutput\">Buint8 =\r\n  uint8\r\n   3\r\n<\/pre><p>Now it's extremely easy to enter in hex or binary values directly into MATLAB.<\/p><pre class=\"codeinput\">D = 0X11\r\nE = 0xF3f\r\nF = 0b10001\r\n<\/pre><pre class=\"codeoutput\">D =\r\n  uint8\r\n   17\r\nE =\r\n  uint16\r\n   3903\r\nF =\r\n  uint8\r\n   17\r\n<\/pre><p>Signed types are available as well.<\/p><pre class=\"codeinput\">G = 0x2As32\r\n<\/pre><pre class=\"codeoutput\">G =\r\n  int32\r\n   42\r\n<\/pre><p>And then you can also represent negative numbers.<\/p><pre class=\"codeinput\">H = 0xFFs8\r\n<\/pre><pre class=\"codeoutput\">H =\r\n  int8\r\n   -1\r\n<\/pre><p>These are represented in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Two%27s_complement\">two's complement form<\/a>.<\/p><p>Since MATLAB is storing these literals as numbers, you can use these arrays anywhere you could have used a traditional numeric array.<\/p><h4>Represent Values as Text<a name=\"4f761765-394c-41ef-b3ce-b2ae00bdc935\"><\/a><\/h4><p>Use <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dec2hex.html\">dec2hex<\/a><\/tt> and <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dec2bin.html\">dec2bin<\/a><\/tt> to convert values to the equivalent hex or binary text.  These are stored in character arrays and can now handle negative values.<\/p><pre class=\"codeinput\">dec2hex(17)\r\ndec2bin(17)\r\ndec2hex(-17)\r\ndec2bin(-17)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n    '11'\r\nans =\r\n    '10001'\r\nans =\r\n    'EF'\r\nans =\r\n    '11101111'\r\n<\/pre><p>Those previous values might be good for annotation.  But the best way to convert your values, especially if you have arrays of them, to hex or binary text is by using <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/compose.html\">compose<\/a><\/tt>, where you get <tt>string<\/tt> output.<\/p><pre class=\"codeinput\">compose(<span class=\"string\">\"%X\"<\/span>, [D,E,F,G,H])\r\n<\/pre><pre class=\"codeoutput\">ans = \r\n  1&times;5 string array\r\n    \"11\"    \"FF\"    \"11\"    \"2A\"    \"0\"\r\n<\/pre><p>You can also use a format specifier in reading functions to read in your data that is represented in literals, either hex or binary.<\/p><h4>Bitwise Operations with Binary Values<a name=\"3b457734-2a6f-4344-8d40-f609a2f8e75d\"><\/a><\/h4><p>You may be in a position where you want to operate on your binary numbers bit by bit.  This is often true for some aspects of interacting with hardware (which I am hardly an expert at!).  These include functions like<\/p><div><ul><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitand.html\">bitand<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitor.html\">bitor<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitshift.html\">bitshift<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitset.html\">bitset<\/a><\/tt><\/li><\/ul><\/div><h4>Summary<a name=\"0322b225-b7bc-43b2-9cb4-3b64af362c51\"><\/a><\/h4><p>Here's more information about <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/bit-wise-operations.html\">bitwise operations<\/a>. Does this make life easier for you?  Let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3739#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_94cc627616b641fdb0a0e6163100ed13() {\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='94cc627616b641fdb0a0e6163100ed13 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 94cc627616b641fdb0a0e6163100ed13';\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        copyright = 'Copyright 2020 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_94cc627616b641fdb0a0e6163100ed13()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020a<br><\/p><\/div><!--\r\n94cc627616b641fdb0a0e6163100ed13 ##### SOURCE BEGIN #####\r\n%% News on the Hexadecimal and Binary Numeric Fronts\r\n% I'm sure you've all heard the joke before:\r\n%\r\n%    There are 10 kinds of people in the world\r\n%    Those who understand binary and those who don't\r\n%\r\n% By the way, the correct pronunciation of the third symbol in the first\r\n% line of the joke is \"two\", not \"ten\".  The hex \"equivalent\" is a bit rude\r\n% so I won't post it here.\r\n%\r\n% Well, no joke now.  We've gotten serious about helping you read or\r\n% specify hex and binary values now, and covering a broader range (i.e.,\r\n% negative hex(!) now.  Let me tell you a bit more.\r\n%\r\n%% Enter Hex and Binary Values\r\n% I don't work with hex and binary data that much myself, but there are\r\n% plenty of people who do. We recently made some updates to how we can\r\n% help you handle such data. Look\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/specify-hexadecimal-and-binary-numbers.html\r\n% here> for the details.\r\n%\r\n% We've always been able to do this:\r\nA = 0.25\r\nB = 3\r\nC = 1\/pi\r\n%%\r\n% and these are all double values in MATLAB.\r\n%%\r\n% |B| could be represented in one of the integer forms. To do so in MATLAB,\r\n% you would either convert, or you might have read in values from some\r\n% other file format and specified the integer type for reading in some\r\n% of the bits.\r\nBuint8 = uint8(B)\r\n%%\r\n% Now it's extremely easy to enter in hex or binary values directly into\r\n% MATLAB.\r\nD = 0X11\r\nE = 0xF3f\r\nF = 0b10001\r\n%%\r\n% Signed types are available as well.\r\nG = 0x2As32\r\n%%\r\n% And then you can also represent negative numbers.\r\nH = 0xFFs8\r\n%%\r\n% These are represented in\r\n% <https:\/\/en.wikipedia.org\/wiki\/Two%27s_complement two's complement form>.\r\n%%\r\n% Since MATLAB is storing these literals as numbers, you can use these\r\n% arrays anywhere you could have used a traditional numeric array.\r\n%% Represent Values as Text\r\n% Use |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/dec2hex.html dec2hex>|\r\n% and |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/dec2bin.html dec2bin>| to\r\n% convert values to the equivalent hex or binary text.  These are stored in\r\n% character arrays and can now handle negative values.\r\ndec2hex(17)\r\ndec2bin(17)\r\ndec2hex(-17)\r\ndec2bin(-17)\r\n%%\r\n% Those previous values might be good for annotation.  But the best way to\r\n% convert your values, especially if you have arrays of them, to hex or\r\n% binary text is by using\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/compose.html compose>|, where\r\n% you get |string| output.\r\ncompose(\"%X\", [D,E,F,G,H])\r\n%%\r\n% You can also use a format specifier in reading functions to read in your\r\n% data that is represented in literals, either hex or binary.\r\n%% Bitwise Operations with Binary Values\r\n% You may be in a position where you want to operate on your binary numbers\r\n% bit by bit.  This is often true for some aspects of interacting with\r\n% hardware (which I am hardly an expert at!).  These include functions like\r\n%\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitand.html bitand>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitor.html bitor>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitshift.html bitshift>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/bitset.html bitset>|\r\n% \r\n%% Summary\r\n% Here's more information about\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/bit-wise-operations.html\r\n% bitwise operations>. Does this make life easier for you?  Let us know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3739#respond here>.\r\n\r\n##### SOURCE END ##### 94cc627616b641fdb0a0e6163100ed13\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>I'm sure you've all heard the joke before:... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2020\/06\/29\/news-on-the-hexadecimal-and-binary-numeric-fronts\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3739"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3739"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3739\/revisions"}],"predecessor-version":[{"id":3743,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3739\/revisions\/3743"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}