# bit-twiddle

> Bit twiddling hacks for JavaScript

Latest version **1.0.2** (published 2014-05-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install bit-twiddle
pnpm add bit-twiddle
yarn add bit-twiddle
bun add bit-twiddle
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2014-05-28 |
| First published | 2013-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/bit-twiddle) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Mikola Lysenko |
| Maintainers | mikolalysenko |
| Keywords | bit, twiddle, hacks, graphics, logarithm, exponent, base 2, binary, arithmetic, octree, quadtree, math, nextPow2, log, shift, combination, permutation, trailing, zero, one, interleave, revere, parity, population, count, exponent, power, sign, min, max |

## Links

- npm: https://www.npmjs.com/package/bit-twiddle
- Repository: https://github.com/mikolalysenko/bit-twiddle
- Issues: https://github.com/mikolalysenko/bit-twiddle/issues
- npm.io page: https://npm.io/package/bit-twiddle

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2014-05-28
- 1.0.1 — 2014-04-30
- 1.0.0 — 2014-04-28
- 0.0.2 — 2013-04-12
- 0.0.1 — 2013-01-16
- 0.0.0 — 2013-01-15

## README

bit-twiddle
===========

This is a collection of miscellaneous bit twiddling hacks ported to JavaScript, mostly taken from here:

* [Stanford Bit Twiddling Hacks](http://graphics.stanford.edu/~seander/bithacks.html)

[![testling badge](https://ci.testling.com/mikolalysenko/bit-twiddle.png)](https://ci.testling.com/mikolalysenko/bit-twiddle)

[![build status](https://secure.travis-ci.org/mikolalysenko/bit-twiddle.png)](http://travis-ci.org/mikolalysenko/bit-twiddle)

Install
=======
Via npm:

    npm install bit-twiddle

# API

### `sign(v)`
Computes the sign of the integer v.  Returns:
* -1 if v < 0
*  0 if v === 0
* +1 if v > 0

### `abs(v)`
Returns the absolute value of the integer v

### `min(x,y)`
Computes the minimum of integers x and y

### `max(x,y)`
Computes the maximum of integers x and y

### `isPow2(v)`
Returns `true` if v is a power of 2, otherwise false.

### `log2(v)`
Returns an integer approximation of the log-base 2 of v

### `log10(v)`
Returns log base 10 of v.

### `popCount(v)`
Counts the number of bits set in v

###  `countTrailingZeros(v)`
Counts the number of trailing zeros.

### `nextPow2(v)`
Rounds v up to the next power of 2.

### `prevPow2(v)`
Rounds v down to the previous power of 2.

### `parity(v)`
Computes the parity of the bits in v.

### `reverse(v)`
Reverses the bits of v.

### `interleave2(x,y)`
Interleaves a pair of 16 bit integers.  Useful for fast quadtree style indexing.  (See wiki: http://en.wikipedia.org/wiki/Z-order_curve )

### `deinterleave2(v, n)`
Deinterleaves the bits of v, returns the nth part.  If both x and y are 16 bit, then it is true that:

```javascript
deinterleave2(interleave2(x,y), 0) === x
deinterleave2(interleave2(x,y), 1) === y
```
    
### `interleave3(x,y,z)`
Interleaves a triple of 10 bit integers.  Useful for fast octree indexing.

### `deinterleave3(v, n)`
Same deal as `deinterleave2`, only for triples instead of pairs

### `nextCombination(x)`
Returns next combination ordered colexicographically.

Acknowledgements
================
Code is ported from Sean Eron Anderson's public domain bit twiddling hacks page.  http://graphics.stanford.edu/~seander/bithacks.html
JavaScript implementation (c) 2013 Mikola Lysenko.  MIT License

---
_Source: https://npm.io/package/bit-twiddle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
