Horje
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 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);

Output
6

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); 

Output
4

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); 

Output
6



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Transform Union Type to Tuple Type in TypeScript ? How to Transform Union Type to Tuple Type in TypeScript ?
JavaScript Program to Extract the Leftmost Set Bit of a Given Integer JavaScript Program to Extract the Leftmost Set Bit of a Given Integer
Compressing String in JavaScript Compressing String in JavaScript
How to Insert Unicode in JavaScript ? How to Insert Unicode in JavaScript ?
How to find out how many Times a Function is Called with JavaScript ? How to find out how many Times a Function is Called with JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15