{"id":105290,"date":"2021-06-08T07:00:00","date_gmt":"2021-06-08T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105290"},"modified":"2021-06-08T07:08:29","modified_gmt":"2021-06-08T14:08:29","slug":"20210608-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210608-00\/?p=105290","title":{"rendered":"The ARM processor (Thumb-2), part 7: Bitwise operations"},"content":{"rendered":"<p>The ARM processor offers the following bitwise operations:<\/p>\n<pre>    ; bitwise and\r\n    and     Rd, Rn, op2         ; Rd = Rn &amp; op2\r\n\r\n    ; bitwise or\r\n    orr     Rd, Rn, op2         ; Rd = Rn | op2\r\n\r\n    ; bitwise exclusive or\r\n    eor     Rd, Rn, op2         ; Rd = Rn ^ op2\r\n\r\n    ; bitwise not\r\n    mvn     Rd, op2             ; Rd = ~op2\r\n\r\n    ; bitwise and not (\"bit clear\")\r\n    bic     Rd, Rn, op2         ; Rd = Rn &amp; ~op2\r\n\r\n    ; bitwise or not\r\n    orn     Rd, Rn, op2         ; Rd = Rn | ~op2\r\n\r\n    ; all support the S suffix\r\n<\/pre>\n<p>For bit-testing purposes, there are also discarding versions:<\/p>\n<pre>    ; test for equivalence\r\n    teq     Rn, op2             ; set flags for Rn ^ op2\r\n\r\n    ; test\r\n    tst     Rn, op2             ; set flags for Rn &amp; op2\r\n<\/pre>\n<p>For bitwise operations that set flags, the negative (N) and zero (Z) flags reflect the result, the carry (C) flag reflects any shifting that occurred during the calculation of <code>op2<\/code> (noting that calculating constants may also involve shifting, as noted earlier when we discussed constants), and the overflow (V) flag is unchanged.<\/p>\n<p>I don&#8217;t see much value in the <code>TEQ<\/code> instruction. It sets the Z flag the same way as the the <code>CMP<\/code> instruction. I guess you could use it to see if two registers have the same sign bit, since it sets N based on the exclusive-or of the two inputs. I guess that&#8217;s handy when calculating the sign of emulated multiplication or division, but even in those cases, you aren&#8217;t going to jump based on the sign; you&#8217;re going to save the sign of the result for later application, so you would be better off with the <code>EOR<\/code> instruction anyway.<\/p>\n<p>Okay, well, you can use the <code>LSL<\/code> shift on the second register argument in order to compare the high bit of one register with an arbitrary bit of another.<\/p>\n<pre>    teq     Rn, Rm LSL #n       ; compare Rn bit 31\r\n                                ; and Rm bit 31 - n\r\n    bmi     same                ; branch if different\r\n<\/pre>\n<p>Still not particularly compelling. Maybe there&#8217;s some specialized workflow where this is useful, like cryptography?<\/p>\n<p>Next time, we&#8217;ll look at the bit shifting instructions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flip-flopping the bits.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-105290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Flip-flopping the bits.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105290"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105290\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}