Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

isPowerOfTwo

  bool isPowerOfTwo(int x)
    {
        // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
        return (x && !(x & (x - 1)));
    }
Comment

isPowerOfTow

const isPowerOfTow =(num)=>{

    if(num === 0){
        return false
    }
   while(num !==1){
        num /=2
    
        if(num %2 !==0 && num !== 1){
            return false
        } 
        
    }
    return true
    
    }

    const isPowerOfTowbitwise =(num)=>{
        return num && !(num&(num-1))

    }

    const powerOfTowRec =(num )=>{

      if(num ===0){
          return false
      }

    
      if(num %2 !== 0 && num !== 1){
          return false

      }
      if(num === 1){
          return true
      }


      return powerOfTowRec(num/2)

    }
    
    const nums=[0,1,2,3,4,6,8,9,256,1024]
    
    nums.forEach( num =>{
        console.log(num,powerOfTowRec(num))
    })
    
Comment

PREVIOUS NEXT
Code Example
Javascript :: Iterate Through the Keys of an Object with a for...in Statement 
Javascript :: JavaScript HTMLCollection Object 
Javascript :: how to store new object made by constructor classes data in local storage using javascript 
Javascript :: Get year from user entered date in javascript 
Javascript :: pass data between router components 
Javascript :: useState increase Likes 
Javascript :: Validation Script Rule 
Javascript :: var countdown = function(num) {} 
Javascript :: Deployment of react static page using node and express 
Javascript :: useEffect : react to manipulate the DOM 
Javascript :: Backbone + Express 
Javascript :: send form data to endpoint js 
Javascript :: simple JSX example 
Javascript :: Initialize View With Collection Backbone 
Javascript :: remove duplicate node 
Javascript :: two way binding in angular 
Javascript :: electron npm start not working 
Javascript :: classes in js 
Javascript :: react native new project 
Javascript :: loop js 
Javascript :: static in class javascript 
Javascript :: instance of javascript 
Javascript :: suitescript dialog box 
Javascript :: exit react native app 
Javascript :: selectize in ajax call 
Javascript :: javascript map size 
Javascript :: how to locate an object with a spcific key in js array 
Javascript :: salesforce set hours javascript 
Javascript :: js regex find newlines 
Javascript :: gatsby js quick start 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =