{"id":556,"date":"2017-10-07T23:43:02","date_gmt":"2017-10-08T06:43:02","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=556"},"modified":"2018-04-19T08:40:42","modified_gmt":"2018-04-19T15:40:42","slug":"leetcode-693-binary-number-with-alternating-bits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-693-binary-number-with-alternating-bits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 693. Binary Number with Alternating Bits"},"content":{"rendered":"<p><strong>Problem:<\/strong><\/p>\n<p>Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: 5\r\nOutput: True\r\nExplanation:\r\nThe binary representation of 5 is: 101\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: 7\r\nOutput: False\r\nExplanation:\r\nThe binary representation of 7 is: 111.\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\">Input: 11\r\nOutput: False\r\nExplanation:\r\nThe binary representation of 11 is: 1011.\r\n<\/pre>\n<p><b>Example 4:<\/b><\/p>\n<pre class=\"\">Input: 10\r\nOutput: True\r\nExplanation:\r\nThe binary representation of 10 is: 1010.<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Bit operation<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms\r\nclass Solution {\r\npublic:\r\n    bool hasAlternatingBits(int n) {\r\n        int r = n &amp; 1;\r\n        while ((n &gt;&gt;= 1) &gt; 0) {\r\n            int t = n &amp; 1;\r\n            if (t == r) return false;\r\n            r = t;            \r\n        }\r\n        return true;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","hentry","category-bit","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"predecessor-version":[{"id":2702,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/556\/revisions\/2702"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}