JavaScript Program to Extract the Rightmost set Bit of a Given Integer
Last Updated :
10 Apr, 2024
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 rightmost set bit in JavaScript.
Using Bitwise Operations
In this approach, we are using the bitwise AND (&) operator with the negation of the input number to isolate the rightmost set bit, effectively extracting it. This works because -num creates a number with only the rightmost set bit of num preserved, allowing the AND operation to capture just that bit.
Syntax:
num & -num
Example: The below example uses Bitwise Operations to extract the rightmost set bit of a given integer in JavaScript.
JavaScript
let num = 32;
let res = num & ~(num - 1);
let position = Math.log2(res);
let output = position + 1;
console.log(output);
Using Bitwise XOR and Complement
In this approach, we are using bitwise XOR (^) and a combination of bitwise AND (&) with subtraction (num - 1) to extract the rightmost set bit of the input number num. The expression (num & (num - 1)) effectively clears all bits to the right of the rightmost set bit, leaving only that bit intact for the XOR operation to isolate it.
Syntax:
let result = operand1 ^ operand2;
Example: The below example uses Bitwise Operations to extract the rightmost set bit of a given integer in JavaScript.
JavaScript
let num = 40;
let res = num ^ (num & (num - 1));
let position = Math.log2(res);
let output = position + 1;
console.log(output);
Using Bitwise Shift and Loop
In this approach, we are using bitwise left shift (<<) in a loop to create a mask that isolates the rightmost set bit of the input number num. The loop iteratively shifts the mask to the left until it aligns with the rightmost set bit in num, which is then extracted using the bitwise AND (&) operation.
Syntax:
while(condition) {
num <<=1
}
Example: The below example uses Bitwise Operations to extract the rightmost set bit of a given integer in JavaScript.
JavaScript
let num = 32;
let mask = 1;
while ((num & mask) === 0) {
mask <<= 1;
}
let position = Math.log2(mask);
let output = position + 1;
console.log(output);
Similar Reads
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 Find the Position of the Rightmost Set Bit Given a number, the task is to find the position of the rightmost set bit. The indexing is from right to left, where the rightmost bit position starts from 1. Example: Input: n = 20Output: 3Explanation: Binary Representation of 20 is 10100, hence position of first set bit from right is 3.Input: n =
3 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
JavaScript Program to Set a Particular Bit in a Number Setting a specific bit at a given position within a number using JavaScript involves manipulating the binary representation of the number to ensure that a particular bit is turned on set to 1. Examples: Input : n = 10, k = 2Output :14ApproachThe variable "bitPosition" determines the index of the bit
2 min read
How to Count Set Bits in an Integer in C++? In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bi
2 min read
Left Shift Operator in Java The decimal representation of a number is a base-10 number system having only ten states 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. For example, 4, 10, 16, etc. The Binary representation of a number is a base-2 number system having only two states 0 and 1. For example, the binary representation of 4, a base-
4 min read