JavaScript Program for Space Optimization Using bit Manipulations Last Updated : 20 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Space optimization using bit manipulations refers to a technique where data is stored in a compact form using individual bits to represent values, reducing memory usage compared to traditional data structures. This method is efficient for managing and storing binary or boolean information, saving space in memory. Examples: Input : 2 10Output : 2 4 5 6 8 10Input: 60 95Output: 60 62 64 65 66 68 70 72 74 75 76 78 80 82 84 85 86 88 90 92 94 95Table of Content Using a bitmaskUsing bitwise operationsApproach 1: Using a bitmaskWe are using a bitmask approach to find unique elements in an input array. It iterates through the array, setting and checking individual bits to determine uniqueness. The uniqueElements array collects unique elements.Example: In the example we see Space optimization using bit manipulations in JavaScript array using bitmask JavaScript let inputArray = [3, 6, 7, 9, 6, 12, 7, 15, 18, 21]; let bitSet = 0; let uniqueElements = []; for (let num of inputArray) { let mask = 1 << num; if ((bitSet & mask) === 0) { uniqueElements.push(num); bitSet |= mask; } } console.log( "Unique Elements using Bit Manipulation:"); // Output: [3, 6, 7, 9, 12, 15, 18, 21] console.log(uniqueElements); OutputUnique Elements using Bit Manipulation: [ 3, 6, 7, 9, 12, 15, 18, 21 ] Approach 2: Using bitwise operationsWe are using bitwise operations to mark multiples of 2 or 5 in an array efficiently. It checks bits for divisibility, sets appropriate bits, and prints multiples from 2 to 10 using bitwise manipulation.Example: In the example we see Space optimization using bit manipulations in JavaScript array using bitwise operations JavaScript function checkbit(array, index) { return array[index >> 5] & (1 << (index & 31)); } // Sets value of bit for corresponding index function setbit(array, index) { array[index >> 5] |= (1 << (index & 31)); } // Driver code let a = 2, b = 10; let size = Math.abs(b - a); size = Math.ceil(size / 32); let array = new Array(size); for (let i = a; i <= b; i++) if (i % 2 == 0 || i % 5 == 0) setbit(array, i - a); console.log("MULTIPLES of 2 and 5:"); for (let i = a; i <= b; i++) if (checkbit(array, i - a)) console.log(i); OutputMULTIPLES of 2 and 5: 2 4 5 6 8 10 Comment More infoAdvertise with us Next Article JavaScript Program to Set a Particular Bit in a Number P parzival_op Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-array JavaScript-DSA JavaScript-Program Geeks Premier League 2023 +3 More Similar Reads 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 Use Bit Manipulation Methods in C++ Bit manipulation is a technique used for optimizing performance and memory usage in various programming scenarios. It is very useful from a Competitive Programming point of view. In this article, we will learn how we can use bit manipulation methods in C++ and provide examples to help you understand 5 min read Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4 5 min read What is Bit Manipulation Bit manipulation is the process of manipulating individual bits or groups of bits in a binary representation of data. It involves performing logical and arithmetic operations on binary digits or bits to achieve a desired result. How to Perform Bit Manipulation?Bit manipulation is performed using bit 4 min read Java Program to Convert Binary Code into Gray Code Without Using Recursion Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits. Gray Code o 4 min read Minimizing Total Manhattan Distances for Driver-Package Allocation Given two integers n and m, where n represents delivery drivers and m represents the number of packages, Additionally, their position are also given in drivers[][] and packages[][] respectively. The task is to allocate each driver a unique package such that the sum of total Manhattan distances betwe 14 min read Like