Check if a given Bit is Set or Not using JavaScript Last Updated : 06 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 with 0 from LSB (least significant bit) side in the binary representation of the number. Example: Input: n = 5, bit_position = 1Output: falseInput: n = 6, bit_position = 2Output: trueBelow are the approaches to check if a given bit is set or not in JavaScript: Table of Content Using Left Shift OperatorUsing Right Shift OperatorUsing Left Shift OperatorThe isBitSet() function which takes two parameter like (number , bitPosition) and here we perform left shift operation to check if a specific bit is set (i.e., equals 1) in a given number. This line first moves the binary representation of the number 1 to the left by bitPosition positions. Then, it performs a bitwise AND operation with the number, checking if the bit at the specified position in the number is set or not. Example: This example shows the implementation of the above approach. JavaScript function isBitSet(number, bitPosition) { return (number & (1 << bitPosition)) !== 0; } const num = 5; // 101 in binary // Check if the least significant bit is set console.log(isBitSet(num, 0)); // Check if the second least significant bit is set console.log(isBitSet(num, 1)); // Check if the most significant bit is set console.log(isBitSet(num, 2)); Outputtrue false true Using Right Shift OperatorThis approach uses the right shift operator (>>) to determine if a specific bit is set in a given number. At first we apply (number >> bitPosition) , this operation shifts the binary representation of the number to the right by bitPosition positions.Then we perform (number >> bitPosition) & 1 == 1 , which is used to check if the bit at the specified position in the binary representation of the number is set (i.e., equals 1).Example: This example shows the implementation of the above approach. JavaScript function isBitSetApproach1(number, bitPosition) { return ((number >> bitPosition) & 1) === 1; } const num = 5; // 101 in binary console.log(isBitSetApproach1(num, 0)); console.log(isBitSetApproach1(num, 1)); console.log(isBitSetApproach1(num, 2)); Outputtrue false true Comment More infoAdvertise with us Next Article JavaScript Program to Extract the Rightmost set Bit of a Given Integer S skaftafh 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 JavaScript Program to Extract the Leftmost Set Bit of a Given Integer We are given an integer value the task is to extract the leftmost set bit of a given integer in JavaScript. The leftmost set bit is a bit whose value is 1 and is present at the leftmost position in binary representation. The below approaches can be implemented to extract the leftmost set bit. Table 2 min read JavaScript Program to Extract the Rightmost set Bit of a Given Integer We are given an integer value, the task is to extract the rightmost set bit of a given integer in JavaScript. The rightmost set bit is the bit whose value is 1 and appears first when reading the binary representation of a number from right to left. The below approaches can be used to find the rightm 3 min read Compute the Bitwise AND of all Numbers in a Range using JavaScript The complexity of bitwise AND computation for all numbers in a specific range is a common challenge faced by developers, particularly when optimizing algorithms. We have given two integers, L and R, and find the bitwise AND of all numbers in the inclusive range from L to R in JavaScript. Example: Le 3 min read JavaScript Program to Check if a Number has Bits in an Alternate Pattern 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: Inpu 2 min read Like