Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including 2complement with all npm packages installed. Try it out:

var _2complement = require("2complement")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

2complement v0.1.1

Go back and forth from 2's complement like a charm

2complement

Go back and forth from 2's complement like a charm in NodeJS

Install

npm i 2complement

Usage

decode = require('2complement');

console.log(decode.int2complement(32765).toString(2));
//1000000000000011

console.log(decode.complement2int(0b1000000000000011))
//-32765

console.log(decode.complement2int(decode.int2complement(-2000)));
//-2000

console.log("You rock");
//just a motivational quote. But you do <3

Methods

  • decode.complement2int(_2complement[,nBits]):
    • _2complement: Number to be converted to int
    • nBits: (optional) Number of bits. 16 is the default
  • decode.int2complement(number[,nBits]):
    • number: Number to be converted to 2's complement
    • nBits: (optional) Number of bits. 16 is the default
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free