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

150
Views
How do I use 32-bit signed integer's complement to represent an integer in javascript

example: I'm going to use 0xC0000000 (32-bit signed complement) for -2^30

const num = Number('0xC0000000')
console.log(num === -Math.pow(2,30)) // expected: true

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

JavaScript only has two's complement integers in two places:

  1. As a temporary value during some operations. The bitwise operators use 32-bit ints, and some Math object methods work with 32-bit int values (for example, imul).

  2. As an element in a Int16Array, Int32Array, or Int64Array. (Int32Array in your case.)

Otherwise, all numbers are either number (IEEE-754 double-precision binary floating point) or BigInt (arbitrary-precision non-two's-complement integers).

So for instance, you can have a two's complement in a single-element Int32Array.

const array = Int32Array.from([0xC000000]);
console.log(array[0].toString(16));

However, whenever you use that 32-bit integer, it gets converted to number. That said, JavaScript's conversions between number and 32-bit signed int are fairly smart. For example, consider this two's complement boundary condition:

const array = Int32Array.from([0x7FFFFFFF]);
console.log(array[0].toString(16)); // 7fffffff
++array[0];
console.log(array[0].toString(16)); // -80000000

That's what you'd want for a two's complement operation, even though it isn't a two's complement operation (in theory; JavaScript engines are allowed to optimize). The operation is 32-bit two's complement int to number, increment number, convert number back to 32-bit two's complement int. But we still get the desired result.

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!