Skip to content

Latest commit

 

History

History
425 lines (90 loc) · 5.6 KB

README.md

File metadata and controls

425 lines (90 loc) · 5.6 KB

🌟 Bit Manipulation in JavaScript 🌟

A collection of bit manipulation helpers written in TypeScript.

This repository is intended for learning bit manipulation in JavaScript/TypeScript. You are welcome to check the source code of each function and the unit tests.

Table of Contents

Points 📌

Common ✍️

  • export const getBinaryString: (num: number) => string;
  • export const parseBinaryString: (str: string) => number;
  • export const convertToUInt32: (num: number) => number;
  • export const swap: (num1: number, num2: number) => number[];

Math 💡

  • export const multiplyBy2: (num: number) => number;
  • export const multiplyBy2PowerN: (num: number, n: number) => number;
  • export const divideBy2: (num: number) => number;
  • export const divideBy2PowerN: (num: number, n: number) => number;
  • export const toggleSign: (num: number) => number;
  • export const addition: (num1: number, num2: number) => number;
  • export const subtraction: (num1: number, num2: number) => number;
  • export const multiplication: (num1: number, num2: number) => number;

Predicates 🔬

  • export const isEven: (num: number) => boolean;
  • export const isOdd: (num: number) => boolean;
  • export const isEquals: (num1: number, num2: number) => boolean;
  • export const isNegative: (num: number) => boolean;
  • export const isPositive: (num: number) => boolean;

Least Significant Bit (LSB) ⚗️

  • export const getLSB: (num: number) => number;
  • export const removeLSB: (num: number) => number;
  • export const setLSB: (num: number) => number;
  • export const unsetLSB: (num: number) => number;
  • export const toggleLSB: (num: number) => number;

Most Significant Bit (MSB) 🤔

  • export const getMSB: (num: number) => number;
  • export const setMSB: (num: number) => number;
  • export const unsetMSB: (num: number) => number;

Working with bits 🧩

  • export const setBitAtPosition: (num: number, n: number) => number;
  • export const unsetBitAtPosition: (num: number, n: number) => number;
  • export const toggleBitAtPosition: (num: number, n: number) => number;
  • export const isBitSetAtPosition: (num: number, n: number) => boolean;
  • export const invertBits: (num: number) => number;
  • export const countSetBits: (num: number) => number;
  • export const countUnsetBits: (num: number) => number;
  • export const getRightmostSetBit: (num: number) => number;

Masks 🎭

  • export const getNSetBitsMask: (n: number) => number;

Number Systems 🔮

  • export const binaryToHex: (num: number) => string;
  • export declare const binaryToDecimal: (nums: number[]) => number;

Practice Problems

LeetCode Solutions

Solutions to LeetCode problems in JavaScript using bit manipulations approach only.


       (                      (    (         
   (   )\ )  *   )  (  (      )\ ) )\ )      
 ( )\ (()/(` )  /(  )\))(   '(()/((()/( (    
 )((_) /(_))( )(_))((_)()\ )  /(_))/(_)))\   
((_)_ (_)) (_(_()) _(())\_)()(_)) (_)) ((_)  
 | _ )|_ _||_   _| \ \((_)/ /|_ _|/ __|| __| 
 | _ \ | |   | |    \ \/\/ /  | | \__ \| _|  
 |___/|___|  |_|     \_/\_/  |___||___/|___|