Urgent.News

What's breaking now, across thousands of outlets.

Tech

Why Two’s Complement Conversion Needs More Than `parseInt`

When I first wrote a signed-binary converter, the obvious implementation was “call toString(2) and let JavaScript do the rest.” That works for positive integers, but it hides the two decisions a useful converter must expose: whether a value fits a chosen width, and which interpretation of the leading bit the reader wants. The Be Good Tool converter keeps those steps explicit. A bit width changes…

When constructing a signed-binary converter, the initial instinct might be to simply call `toString(2) and let JavaScript handle the rest.” This approach is effective for positive integers, yet it conceals two crucial decisions that any practical converter must make: the valid range for a given bit width and the interpretation of the leading bit by the reader. The "Be Good Tool" converter addresses these concerns by making these steps explicit.

The converter first determines the valid range for a given number of signed bits. For N signed bits, the range is from -2^(N-1) through 2^(N-1)-1. Before formatting the output, the function `limitNum` checks if the input value falls within this range. If the value exceeds the maximum or falls below the minimum, it is set to 0 or the respective boundary value.

If the value lies outside the signed range but within the unsigned boundary, a different check result is returned. This distinction enables the UI to provide clearer feedback to the user, avoiding silent wrapping of invalid values.

Another important aspect to consider is padding, which plays a significant role in the binary representation. When converting a number to its binary form, the magnitude is used: `toBinary` converts a number to its binary representation, disregarding any sign. The function `fillBitWith_0_Signed` then pads the binary string with leading zeros to reach the desired bit length.

Padding is not merely cosmetic; it ensures that the correct bit becomes the sign bit, ultimately altering the meaning of subsequent operations. For instance, the eight-bit two's complement representation of the number 5 is 00000101, while that of -5 is 11111011. The selected bit width directly affects which bit serves as the sign bit and, consequently, the interpretation of the entire number.

Both one's complement and two's complement are essentially string algorithms that manipulate the binary digits as characters. The source code maintains the bits as strings to preserve leading zeros, which would otherwise be lost with standard numeric representations. To perform one's complement, the function `notString` flips each bit in the binary string.

Conversely, to convert from one's complement to two's complement, the `one2two` function walks from the rightmost bit, turning trailing ones into zeros until it encounters a zero, which it then changes into a one. This process closely resembles the carry propagation mechanism found in hardware adders, but is explicitly demonstrated in JavaScript.

When converting back to a decimal number, the choice of interpretation becomes paramount. For a two's-complement bit string, the first bit carries a negative weight. The function `bin2Dec_2` performs this conversion by first splitting the binary string into an array of bits, then determining the value of the first bit based on its weight (-2^(N-1)) and adding the weighted values of the remaining bits accordingly.

The UI also reports the standard `parseInt(numBin, 2)`, as well as the one's complement conversion method, offering users multiple ways to interpret the same binary representation.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How to Automate Lead Follow-Up Without Sounding Automated

TL;DR: do not automate sending, automate the blank page. A rep writing follow-ups by hand manages about 20 a day; reviewing and approving drafts they manage 80 with the same personalisation.

  • Automate draft stage, not entire email sending
  • Use CRM and model to generate personalized follow-ups
  • Approval step ensures genuine, non-automated tone

More from Saturday 10 October →