Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
JavaScript BigInt print unsigned binary represenation

How do you print an unsigned integer when using JavaScript's BigInt?

BigInts can be printed as binary representation using toString(2). However for negative values this function just appends a - sign when printing.

BigInt(42).toString(2)
// output => 101010
BigInt(-42).toString(2)
// output => -101010

How do I print the unsigned representation of BigInt(42)? I that with regular numbers you can do (-42 >>> 0).toString(2), however the unsigned right shift seems not to be implemented for BigInt, resulting in an error

(BigInt(-42) >>> BigInt(0)).toString(2)
// TypeError: BigInts have no unsigned right shift, use >> instead
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

An easy way to get the two's complement representation for negative BigInts is to use BigInt.asUintN(bit_width, bigint):

> BigInt.asUintN(64, -42n).toString(2)
'1111111111111111111111111111111111111111111111111111111111010110'

Note that:

  • You have to define the number of bits you want (64 in my example), there is no "natural"/automatic value for that.
  • Given only that string of binary digits, there is no way to tell whether this is meant to be a positive BigInt (with a value close to 2n**64n) or a two's complement representation of -42n. So if you want to reverse the conversion later, you'll have to provide this information somehow (e.g. by writing your code such that it implicitly assumes one or the other option).
  • Relatedly, this is not how -42n is stored internally in current browsers. (But that doesn't need to worry you, since you can create this output whenever you want/need to.)
  • You could achieve the same result with a subtraction: ((2n ** 64n) - 42n).toString(2) -- again, you can specify how many bits you'd like to see.

Is there something like bitAtIndex for BigInt?

No, because there is no specification for how BigInts are represented. Engines can choose to use bits in any way they want, as long as the resulting BigInts behave as the specification demands.


@Kyroath:

negative BigInts are represented as infinite-length two's complement

No, they are not: the implementations in current browsers represent BigInts as "sign + magnitude", not as two's complement. However, this is an unobservable implementation detail: implementations could change how they store BigInts internally, and BigInts would behave just the same.

What you probably meant to say is that the two's complement representation of any negative integer (big or not) is conceptually an infinite stream of 1-bits, so printing or storing that in finite space always requires defining a number of characters/bits after which the stream is simply cut off. When you have a fixed-width type, that obviously defines this cutoff point; for conceptually-unlimited BigInts, you have to define it yourself.

about 4 years ago · Juan Pablo Isaza Report

0

Here's a way to convert 64-bit BigInts into binary strings:

// take two's complement of a binary string
const twosComplement = (binaryString) => {
  let complement = BigInt('0b' + binaryString.split('').map(e => e === "0" ? "1" : "0").join(''));
  return decToBinary(complement + BigInt(1));
}

const decToBinary = (num) => {
  let result = ""

  const isNegative = num < 0;
  if (isNegative) num = -num;

  while (num > 0) {
    result = (num % BigInt(2)) + result;
    num /= BigInt(2);
  }

  if (result.length > 64) result = result.substring(result.length - 64);
  result = result.padStart(64, "0");

  if (isNegative) result = twosComplement(result);
  return result;
}

console.log(decToBinary(BigInt(5))); // 0000000000000000000000000000000000000000000000000000000000000101
console.log(decToBinary(BigInt(-5))); // 1111111111111111111111111111111111111111111111111111111111111011

This code doesn't do any validation, however.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!