{"id":589,"date":"2017-02-02T12:56:36","date_gmt":"2017-02-02T10:56:36","guid":{"rendered":"http:\/\/www.aquilin.nl\/?page_id=589"},"modified":"2017-02-02T12:56:36","modified_gmt":"2017-02-02T10:56:36","slug":"bit-manipulation","status":"publish","type":"page","link":"https:\/\/ese.han.nl\/~harends\/wpsite\/?page_id=589","title":{"rendered":"Bit manipulation"},"content":{"rendered":"<p>Manipulating individual bits in registers is important in microcontroller programming. Data is always read and written in parallel, also when there is just one bit of interest.\u00a0Logic\u00a0and bitwise operators are used to mask the bits that are not of interest and are therefore a key factor for the domain of microcontroller programming.<\/p>\n<h1>Register<\/h1>\n<p>Bits are most often directly manipulated in registers. Here is an\u00a0example register from the STM32F05x microcontroller with a random value in hexadecimal format:<\/p>\n<pre><b>GPIOC-&gt;ODR: 0xFF0000FF<\/b><\/pre>\n<p>We could clear bit 0 of this register like this:<\/p>\n<pre><b>GPIOC-&gt;ODR = 0x00000000;<\/b><\/pre>\n<p>But as you can see, it also clears all the other bits. So before updating a register, we will read it&#8217;s content first and then modify it by using a mask, which is also known as a\u00a0<em>read-modify-write <\/em>cycle<em>.<\/em><\/p>\n<h1>Mask<\/h1>\n<p>A mask makes sure that we write or read only the bit(s) of interest. Here is an example of a mask with 16 bits cleared and 16 bits sets:<\/p>\n<pre>MASK = 0x0000FFFF;<\/pre>\n<p>The operator used with the mask determines the result of the operation.<\/p>\n<h1>Individual bit set: bitwise-or<\/h1>\n<p><strong><span style=\"color: #339966;\"><em>Sets the bits in REG that are set in MASK, all other bits are unchanged<\/em><\/span><\/strong><\/p>\n<pre><strong><span style=\"color: #339966;\">REG = REG | MASK;\r\n\r\nREG |= MASK;<\/span><\/strong><\/pre>\n<p>Example evaluation:<\/p>\n<pre>GPIOC-&gt;ODR = GPIOC-&gt;ODR | 0x0000FFFF;\r\nGPIOC-&gt;ODR = 0xFF0000FF | 0x0000FFFF;\r\nGPIOC-&gt;ODR = 0xFF00FFFF;<\/pre>\n<h1>Individual bit clear: bitwise-and with inverse<\/h1>\n<p><span style=\"color: #ff9900;\"><strong><em>Clears the bits in REG that are set in MASK, all other bits are unchanged<\/em><\/strong><\/span><\/p>\n<pre><span style=\"color: #ff9900;\"><b>REG = REG &amp; (~MASK);\r\n\r\nREG &amp;= ~MASK;<\/b><\/span><\/pre>\n<p>Example evaluation:<\/p>\n<pre>GPIOC-&gt;ODR = GPIOC-&gt;ODR &amp; (~0x0000FFFF);\r\nGPIOC-&gt;ODR = 0xFF0000FF &amp; (~0x0000FFFF);\r\nGPIOC-&gt;ODR = 0xFF0000FF &amp;   0xFFFF0000;\r\nGPIOC-&gt;ODR = 0xFF000000;<\/pre>\n<h1>Individual bit toggle: bitwise-xor<\/h1>\n<p><span style=\"color: #3366ff;\"><strong><em>Toggles the bits in REG that are set in MASK, all other bits are unchanged<\/em><\/strong><\/span><\/p>\n<pre><span style=\"color: #3366ff;\"><b>REG = REG ^ MASK;\r\n\r\nREG ^= MASK;<\/b><\/span><\/pre>\n<p>Example evaluation:<\/p>\n<pre>GPIOC-&gt;ODR = GPIOC-&gt;ODR ^ 0x0000FFFF;\r\nGPIOC-&gt;ODR = 0xFF0000FF ^ 0x0000FFFF;\r\nGPIOC-&gt;ODR = 0xFF00FF00;<\/pre>\n<h1>Check the status of an individual bit<\/h1>\n<p>To check\u00a0the status of a bit, a mask is used to ignore all bits not of interest.<br \/>\nLet&#8217;s check if bit 0 in the register GPIOA-&gt;IDR is set. Then the mask must be 0x00000001.<\/p>\n<pre>if(GPIOA-&gt;IDR &amp; 0x00000001)\r\n{\r\n    \/\/ Yes, bit is set\r\n}\r\nelse\r\n{\r\n    \/\/ No, bit is not set\r\n}<\/pre>\n<p>Notice how the mask and the bitwise-and operator make sure that all bits not of interest will be 0 for sure! If bit 0 in GPIOA-&gt;IDR is also 0, the condition will yield 0, which is equivalent to false. If bit 0 in GPIOA-&gt;IDR is 1, the condition will not be 0, which is equivalent to true.<\/p>\n<p>We can also check if bit 0 is cleared \ud83d\ude42<\/p>\n<pre>if(<strong>!<\/strong>(GPIOA-&gt;IDR &amp; 0x00000001))\r\n{\r\n    \/\/ Yes, bit is cleared\r\n}\r\nelse\r\n{\r\n    \/\/ No, bit is not cleared\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Manipulating individual bits in registers is important in microcontroller programming. Data is always read and written in parallel, also when there is just one bit of interest.\u00a0Logic\u00a0and bitwise operators are used to mask the bits that are not of interest &hellip; <a href=\"https:\/\/ese.han.nl\/~harends\/wpsite\/?page_id=589\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":554,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/pages\/589"}],"collection":[{"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=589"}],"version-history":[{"count":0,"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/pages\/589\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=\/wp\/v2\/pages\/554"}],"wp:attachment":[{"href":"https:\/\/ese.han.nl\/~harends\/wpsite\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}