# Two Simple Approaches with Explanations

• ### Approaches:

1. Bit Manipulation
2. Regex

#### 1. Bit Manipulation:

The comments tell it all. We bit shift once, add it to the original, and check to see if adding one to that will result in a power of 2.

• note how we check for a power of two using: ( num & num + 1 === 0 ). For details, check out this Stack Overflow Question
``````function bitSolution(number){
//      10101010101
//  +    1010101010    ( number >> 1 )
//  ---------------
//  =   11111111111    ( this is stored in the variable 'tmp' )
//  &  100000000000
//  ---------------
//  =             0    ( power of two )
let tmp = ( number >> 1 ) + number;
return( (tmp & tmp + 1) === 0);
}
``````

#### 2. Regex:

• the !! is a javascript trick. It returns a boolean. AKA - "if the object is not null".
• When you call toString(..) on an integer, you pass the base as the parameter. In this case, base 2 for binary.

The regular expression itself is pretty standard.

``````function regexSolution(number){
// starts with 1
// { w{0,1}*: character cannot occur twice in a row }
return !!/^(10)*1?\$/g.test((n).toString(2));
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.