JavaScript Program to Check if a Number has Bits in an Alternate Pattern Last Updated : 08 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report JavaScript can be used to assess whether a given number follows an alternating pattern in its binary representation. By examining the binary digits of the number, one can determine if the sequence alternates between 0s and 1s, aiding in understanding the binary structure of the input. Examples: Input : 15Output : NoExplanation: Binary representation of 15 is 1111.Input : 10Output : YesExplanation: Binary representation of 10 is 1010.Below are the methods to check if a number has bits in an alternate pattern: Table of Content Using binary conversionUsing bitwise manipulationUsing binary conversionIn this approach iterates through each bit of the number's binary representation, comparing each bit with the previous one. If any two adjacent bits are the same, it returns false; otherwise, it returns true. Example: To demonstrate checking if a number has bits in an alternate pattern or not. JavaScript function hasAltBitPattern(n) { let prev = n % 2; n = Math.floor(n / 2); while (n > 0) { let curr = n % 2; if (curr === prev) return false; prev = curr; n = Math.floor(n / 2); } return true; } let numToCheck = 10; if (hasAltBitPattern(numToCheck)) console.log("Yes"); else console.log("No"); OutputYes Time Complexity: O(log2n) Auxiliary Space: O(1) Using bitwise manipulationIn this approach we perform a bitwise AND operation between the number and its right-shifted version by one bit. If the result is zero, it means there are no common set bits between adjacent bits, indicating an alternate bit pattern. Otherwise, it returns false. Example: To demonstrate checking if a number has bits in an alternate pattern. JavaScript function hasAltBitPattern(n) { if (n > 1) { return (n & (n >> 1)) === 0; } else { return false; } } let numToCheck = 100; if (hasAltBitPattern(numToCheck)) { console.log("Yes"); } else { console.log("No"); } OutputYes Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article JavaScript Program to Check if a Number is Sparse or Not A abhay94517 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Check if a Number is Sparse or Not A number is considered sparse if it does not have any consecutive ones in its binary representation. Example: Input: n=21Output: TrueExplanation: there are no consecutive 1s in the binary representation (10101). Therefore, 21 is a sparse number.Input: n=22Output: FalseExplanation: there are consecut 3 min read JavaScript Program to Check if all Bits can be made Same by Single Flip In this article, we will explore how to determine if it's possible to make all bits the same in a binary string by performing a single flip operation. We will cover various approaches to implement this in JavaScript and provide code examples for each approach.Examples:Input: 1101Output: YesExplanati 5 min read Check if a given Bit is Set or Not using JavaScript Bits in a number are fundamental components of binary representation and each bit in binary has only two states like 0 or 1. With the help of those two bits, we can represent any number in a binary format. And if a bit is to be set if it's value is 1 otherwise it is not set. Note: Indexing starts wi 2 min read Check if the ith Bit is Set or Not using JavaScript Checking if the ith bit is set or not in a binary representation is a common task in programming, especially in scenarios where bitwise operations are utilized. In this article, we'll explore multiple approaches to accomplish this using JavaScript. Given a non-negative integer num and an index i, we 2 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Find the Number Using Bitwise Questions I Given a task to find a number n. There is a pre-defined API int commonSetBits(int val) that returns the number of bits where both n and val have a value of 1 in the corresponding position of their binary representation. In other words, it returns the number of set bits in the bitwise AND (&) ope 4 min read Like