JavaScript Program for Space Optimization Using bit Manipulations Last Updated : 20 Nov, 2023 Comments Improve Suggest changes 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 info 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 Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like