JavaScript Program to Set a Particular Bit in a Number Last Updated : 13 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 to be set, starting from 0.A bit mask is created using left shift ('<<'), resulting in a mask like "00000100" for 'bitPosition = 2'.The variable "n" is assigned a decimal value, such as "10", which corresponds to its binary representation "00001010".Bitwise OR ('|') operation is applied between 'n' and the mask to set the specified bit to 1, leaving other bits unchanged.The output will be 14, representing the decimal equivalent of the binary number with the specified bit set.Example: The example below shows how to set a particular bit in a number using JavaScript. JavaScript // Define the bit position to set (0-based index) let bitPosition = 2; // 3rd bit (0-based index) // Create a bit mask with only the desired bit set to 1 // This creates a mask like 00000100 (for bitPosition=2) let mask = 1 << bitPosition; // Binary: 00001010 let n = 10; // Set the specified bit using bitwise OR n |= mask; // Binary: 00001010 | 00000100 = 00001110 (14 in decimal) console.log(n); Output14 Time Complexity: O(1). Space Complexity: O(1). Comment More infoAdvertise with us Next Article JavaScript Program to Set a Particular Bit in a Number L lunatic1 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Convert a Number to Binary We are going to learn the conversion of a number to binary by using JavaScript, Convert a number to binary in JavaScript refers to the process of converting a decimal number into its binary representation, which uses only the digits 0 and 1 to represent the value Converting a number to binary in Jav 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 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 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 Convert Decimal to Binary In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values. Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to i 5 min read Like