Created
March 6, 2025 13:29
-
-
Save Angelfire/8331377f6d989d4fa6f35089aad767c8 to your computer and use it in GitHub Desktop.
functions using bitwise operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* High-Performance JavaScript Bitwise Operations Library | |
* A collection of efficient functions using bitwise operators to replace common JavaScript operations | |
*/ | |
/** | |
* Converts the target value to an integer using the bitwise OR operator. | |
* ~76% faster than parseInt(target, 10) | |
* | |
* @param {string|number} target - The target number to parse to integer | |
* @returns {number} The integer value | |
*/ | |
const parseInteger = (target) => target | 0; | |
/** | |
* Checks if a number is even using bitwise AND. | |
* | |
* @param {number} target - The target number to test | |
* @returns {boolean} True if even, false if odd | |
*/ | |
const isEven = (target) => !(target & 1); | |
/** | |
* Checks if a number is odd using bitwise AND. | |
* | |
* @param {number} target - The target number to test | |
* @returns {boolean} True if odd, false if even | |
*/ | |
const isOdd = (target) => !!(target & 1); | |
/** | |
* Rounds a number down to the nearest integer (floor). | |
* Faster than Math.floor(). | |
* | |
* @param {number} target - The number to round down | |
* @returns {number} The rounded down integer | |
*/ | |
const floor = (target) => target | 0; | |
/** | |
* Rounds a number to the nearest integer. | |
* Uses the trick of adding 0.5 before flooring. | |
* | |
* @param {number} target - The number to round | |
* @returns {number} The rounded integer | |
*/ | |
const round = (target) => (target + 0.5) | 0; | |
/** | |
* Fast absolute value calculation. | |
* Works for 32-bit integers only. | |
* | |
* @param {number} target - The number to get absolute value of | |
* @returns {number} Absolute value | |
*/ | |
const abs = (target) => (target ^ (target >> 31)) - (target >> 31); | |
/** | |
* Fast integer division by power of 2. | |
* Much faster than regular division for powers of 2. | |
* | |
* @param {number} target - The number to divide | |
* @param {number} power - The power of 2 to divide by (e.g., 1 for 2, 2 for 4, 3 for 8) | |
* @returns {number} The result of the division | |
*/ | |
const divideByPowerOf2 = (target, power) => target >> power; | |
/** | |
* Fast integer multiplication by power of 2. | |
* | |
* @param {number} target - The number to multiply | |
* @param {number} power - The power of 2 to multiply by (e.g., 1 for 2, 2 for 4, 3 for 8) | |
* @returns {number} The result of the multiplication | |
*/ | |
const multiplyByPowerOf2 = (target, power) => target << power; | |
/** | |
* Get the maximum value of two integers. | |
* | |
* @param {number} a - First number | |
* @param {number} b - Second number | |
* @returns {number} The larger number | |
*/ | |
const max = (a, b) => a - ((a - b) & ((a - b) >> 31)); | |
/** | |
* Get the minimum value of two integers. | |
* | |
* @param {number} a - First number | |
* @param {number} b - Second number | |
* @returns {number} The smaller number | |
*/ | |
const min = (a, b) => b + ((a - b) & ((a - b) >> 31)); | |
/** | |
* Fast modulo operation for powers of 2. | |
* For example, x % 8 is equivalent to x & 7 | |
* | |
* @param {number} target - The number to get modulo of | |
* @param {number} divisor - The divisor (must be power of 2) | |
* @returns {number} The remainder | |
*/ | |
const modPowerOf2 = (target, divisor) => target & (divisor - 1); | |
/** | |
* Swaps two values without using a temporary variable. | |
* | |
* @param {Array} array - Array containing the two values to swap | |
* @param {number} i - Index of first value | |
* @param {number} j - Index of second value | |
*/ | |
const swapInPlace = (array, i, j) => { | |
if (i !== j) { | |
array[i] ^= array[j]; | |
array[j] ^= array[i]; | |
array[i] ^= array[j]; | |
} | |
}; | |
/** | |
* Checks if a number is a power of 2. | |
* | |
* @param {number} target - The number to check | |
* @returns {boolean} True if the number is a power of 2 | |
*/ | |
const isPowerOf2 = (target) => !!target && !(target & (target - 1)); | |
/** | |
* Counts the number of bits set to 1 in an integer (population count). | |
* | |
* @param {number} target - The number to count bits in | |
* @returns {number} The number of bits set to 1 | |
*/ | |
const countBits = (target) => { | |
target = target - ((target >> 1) & 0x55555555); | |
target = (target & 0x33333333) + ((target >> 2) & 0x33333333); | |
return ((target + (target >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; | |
}; | |
/** | |
* Checks if two numbers have opposite signs. | |
* | |
* @param {number} a - First number | |
* @param {number} b - Second number | |
* @returns {boolean} True if the numbers have opposite signs | |
*/ | |
const hasOppositeSigns = (a, b) => !!(a ^ b) >> 31; | |
/** | |
* Calculates the average of two integers without overflow. | |
* | |
* @param {number} a - First number | |
* @param {number} b - Second number | |
* @returns {number} The average of the two numbers | |
*/ | |
const average = (a, b) => (a & b) + ((a ^ b) >> 1); | |
module.exports = { | |
parseInteger, | |
isEven, | |
isOdd, | |
floor, | |
round, | |
abs, | |
divideByPowerOf2, | |
multiplyByPowerOf2, | |
max, | |
min, | |
modPowerOf2, | |
swapInPlace, | |
isPowerOf2, | |
countBits, | |
hasOppositeSigns, | |
average | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment