Check if the ith Bit is Set or Not using JavaScript Last Updated : 13 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Checking if the ith bit is set or not in a binary representation is a common task in programming, especially in scenarios where bitwise operations are utilized. In this article, we'll explore multiple approaches to accomplish this using JavaScript. Given a non-negative integer num and an index i, we need to determine whether the ith bit (from the right) of num is set (1) or not (0). These are the following approaches: Table of Content Using Bitwise AND OperationUsing Right Shift and Bitwise AND OperationUsing Bitwise Shift and Modulus OperatorUsing Bitwise AND OperationThis approach checks if the ith bit is set by using the bitwise AND operator (&). If the ith bit is set, the result of the bitwise AND operation will be non-zero; otherwise, it will be zero. Example: Implementation to check if the ith bit is set or not using bitwise AND operation. JavaScript function isIthBitSet(num, i) { let mask = 1 << i; // This line checks if the ith bit is set return (num & mask) !== 0; } console.log(isIthBitSet(5, 1)); Outputfalse Time Complexity: O(1) Space Complexity: O(1) Using Right Shift and Bitwise AND OperationWith this approach, we generate the mask with a right shift compared to a left one. This approach works especially well when the LSB (least significant bit) is considered as the 0th bit. Example: Implementation to check if the ith bit is set or not using right shift and bitwise AND operation. JavaScript function isIthBitSet(num, i) { let mask = num >> i; return (mask & 1) === 1; } console.log(isIthBitSet(7, 1)); Outputtrue Time Complexity: O(1) Space Complexity: O(1) Using Bitwise Shift and Modulus OperatorThe approach involves shifting the given number num by i bits to the right, after which we evaluate if the result is odd or even by using the modulus operator. The ith bit is set if it is odd. Example: Implementation to check if the ith bit is set or not using bitwise Shift and modulus operator. JavaScript function isIthBitSet(num, i) { return (num >> i) % 2 === 1; } console.log(isIthBitSet(5, 1)); Outputfalse Time Complexity: O(1) Space Complexity: O(1) Comment More infoAdvertise with us Next Article Check if the ith Bit is Set or Not using JavaScript H heysaiyad Follow Improve Article Tags : JavaScript Web Technologies JavaScript Programs Similar Reads Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read How to check a URL contains a hash or not using JavaScript ? In this article, the task is to check whether an URL contains or not. This can be done by using the Location hash property in JavaScript. It returns the string which represents the anchor part of a URL including the hash â#â sign. Syntax: window.location.hash Example: This example uses the hash prop 1 min read Detect a device is iOS or not using JavaScript In order to detect a device whether it is iOS or not. We're going to Navigator platform and Navigator userAgent property. Navigator userAgent property This property returns the value of the user-agent header which is sent by the browser to the server. Returned value, have information about the name, 2 min read How to check if CSS property is supported in browser using JavaScript ? Few CSS properties are not supported in some browsers, thus it is required to check whether a CSS property & its value are supported in the current browser. We can find it using JavaScript using CSS.supports() method. Syntax: supports(propertyName, value) supports(condition) There are two distin 2 min read Detect the Operating System of User using JavaScript The task is detect the operating system of User with the help of JavaScript. we're going to discuss few techniques. Approach: Access the navigator.appVersion or navigator.userAgent property. Get the index of the OS using indexOf() method. If index is other than -1, then it is the OS, that we are loo 2 min read Like