693. Alternate Bit Binary Numbers (Bitwise Operations: Bitwise AND &, XOR)

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to viewactivity details

The 75th day of daily brushing 2021.03.28

693. Alternate Bit Binary Numbers

Topic description

  • Given a positive integer, check that its binary representation always alternates 0s and 1s: in other words, the two adjacent digits in the binary representation are never the same.

Example

  • Example 1
输入: n = 5
输出: true
解释: 5 的二进制表示是:101
复制代码
  • Example 2
输入: n = 7
输出: false
解释: 7 的二进制表示是:111.
复制代码

Problem solving ideas

similar topics

regular simulation

  • After converting the current decimal number into a binary number, separate the last digit and the previous digit for comparison. If the two bits are always the same, it is the matching alternate bit binary number, and return true; otherwise, return directlyfalse
  • The original idea: Modulo operation obtains the last digit, and no two bits are compared as a group. If there is a group that does not meet the requirements, then return false; otherwise, if no incompatibility is found, returntrue
  • Then you only need to use the XOR operator to determine whether it matches
    • XOR, 1
    • same XOR, 0

post-optimization

  • Each time you get the last digit value ( 0或者1), you don't need to use the modulo operation, you can use &the bitwise AND operation
  • Example:
    • 111 & 1= 1
    • 110 & 1= 0
  • Summary: Bitwise AND is used to determine whether the last bit is 0还是1yes
  • In terms of operation speed, bit operation is better than modulo operation.

Another bitwise method

  • nAfter the binary representation of the input is shifted to the right by one bit, the resulting number is XORed with nthe bitwise OR a. If and only if the input nis an alternate-bit binary number, athe binary representation is all 1(excluding leading 0). Here is a simple proof: awhen a bit of 1is , if and only if nthe corresponding bit of is different from its previous bit. aWhen each bit of 1is , if and only nif all adjacent bits of are different, is nan alternating-bit binary number.
  • Bitwise AND the and if and only if the abinary representation of is all , the result is . Here is a simple proof: if and only if the binary representation of is all , you can carry out, and the original highest position is , and the result of the bitwise AND is . Otherwise, no carry will be generated, the two most significant bits are both , and the phase and result are not . Combining the above two steps, it can be judged whether the input is an alternate bit binary number.a + 1a10a1a + 10010

ACcode

var hasAlternatingBits = function(n) {
  let contra = n % 2;
  n = n >> 1;
  while(n) {
    let wei = n % 2;
    if(!((wei) ^ contra)){
      return false;
    }
    // console.log('contra:', contra)
    contra = wei;
    n = n >> 1;
  }
  return true;
};
复制代码

New bit operation solution

var hasAlternatingBits = function(n) {
    const a = n ^ (n >> 1);
    return (a & (a + 1)) === 0;
};
复制代码

Summarize

  • Shift operators are inherently faster n / 2than
    • shift operator =parseInt(n / 2)

Guess you like

Origin juejin.im/post/7080161038393409544