JavaScript Program to Extract the Leftmost Set Bit of a Given Integer Last Updated : 10 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 of Content Using Bitwise OperationsUsing LogarithmsUsing Binary String ConversionUsing Bitwise OperationsIn this approach, we will use the left shift (<<) and unsigned right shift (>>>) bitwise operations to initialize and update a mask (res) with the leftmost set bit of the given integer num. The while loop checks and shifts the mask until it aligns with the leftmost set bit in num, resulting in res containing only the leftmost set bit value. Syntax:x << yExample: The below example uses Bitwise Operations to extract the leftmost set bit of a given integer in JavaScript. JavaScript let num = 18; let res = 1 << 31; while ((res & num) === 0) { res = res >>> 1; } console.log(res); Output16 Using LogarithmsIn this approach, we are using logarithms (Math.log2) to find the position of the leftmost set bit in the given integer, and then using Math.pow to calculate the value of that bit, resulting in res containing only the leftmost set bit value. Syntax:Math.log2(x)Example: The below example uses Logarithms to extract the leftmost set bit of a given integer in JavaScript. JavaScript let num = 40; let res = Math.pow(2, Math.floor(Math.log2(num))); console.log(res); Output32 Using Binary String ConversionIn this approach, we are using binary string conversion (toString(2)) to represent the integer number in binary form. Then, we calculate the position of the leftmost set bit by finding the index of the rightmost '1' in the binary string. Finally, we use a bitwise left shift (<<) to create a mask (res) with only the leftmost set bit value. Syntax:num.toString(2)Example: The below example uses Binary String Conversion to extract the leftmost set bit of a given integer in JavaScript. JavaScript let num = 9; let bStr = num.toString(2); let lSetBitPos = bStr.length - 1 - bStr. indexOf('1'); let res = 1 << lSetBitPos; console.log(res); Output8 Comment More infoAdvertise with us Next Article How to Count Set Bits in an Integer in C++? A anjalibo6rb0 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 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 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 Extract Bits in C++ Extracting bits from a given number involves extracting 'k' bits starting from a specified position 'pos' by using bitwise operations like AND (&) and shifts (<<, >>). In this article, we will learn how to extract bits in C++.Example:Input:num = 214k = 3pos = 2Output:5Extracting Bits 2 min read Like