Two's Complement Converter
Signed integer ↔ raw hex at 8/16/32-bit widths.
This tool converts between signed decimal integers and their raw two's-complement representation at 8, 16 or 32-bit width — with unsigned and binary views of the same bit pattern. The reference case: 0xFFF6 read as a signed 16-bit value is −10, while the same bits read unsigned are 65526.
How it works
Two's complement stores a negative number −x as 2ᴺ − x in an N-bit word — equivalently, invert all bits of x and add one. The most significant bit then acts as the sign: patterns with it set decode as value − 2ᴺ. The genius of the scheme is that addition, subtraction and comparison against zero all work with plain unsigned adder hardware, so every mainstream CPU and PLC uses it for signed integers.
Problems appear at the boundaries between systems: a Modbus register is just 16 bits, and whether those bits mean 65526 or −10 is purely a matter of interpretation the protocol does not carry. Editing either field here updates the other, so you can check both directions instantly.
Worked example
|−10| = 0x000A → invert → 0xFFF5 → +1 → 0xFFF6
unsigned reading: 65526 · binary: 1111 1111 1111 0110
Parameters
| Parameter | Value | Notes |
|---|---|---|
| 8-bit range | −128 … 127 | 0x80 … 0x7F |
| 16-bit range | −32 768 … 32 767 | 0x8000 … 0x7FFF |
| 32-bit range | −2 147 483 648 … 2 147 483 647 | |
| Negation | invert bits, add 1 | |
| Sign bit | most significant bit |
C implementation
#include <stdint.h>
/* Reinterpret a raw 16-bit register as signed (two's complement). */
int16_t as_signed16(uint16_t raw)
{
return (int16_t)raw; /* 0xFFF6 -> -10 */
}
/* Portable form without implementation-defined casts: */
int32_t to_signed(uint32_t v, int bits)
{
uint32_t sign = 1u << (bits - 1);
return (v & sign) ? (int32_t)(v - (1u << bits)) : (int32_t)v;
}FAQ
▸Why does my register read 65526 when the sensor says -10?
Because the register is being read as unsigned. The 16-bit pattern 0xFFF6 is 65526 unsigned but −10 in two's complement. Reading signed values as unsigned (or vice versa) is one of the most common Modbus and PLC integration bugs — this converter shows both interpretations of the same bits side by side.
▸How does two's complement encode negative numbers?
A negative number −x is stored as 2ᴺ − x for an N-bit word. Equivalently: invert every bit of x and add one. The top bit doubles as the sign indicator, and arithmetic works with the same adder hardware as unsigned math — which is why virtually every CPU uses it.
▸What are the value ranges per width?
8-bit: −128 … 127. 16-bit: −32768 … 32767. 32-bit: −2147483648 … 2147483647. The asymmetry (one more negative value than positive) comes from zero occupying a non-negative pattern.
▸How do I sign-extend a 16-bit value to 32 bits?
Copy the sign bit into all upper bits: 0xFFF6 becomes 0xFFFFFFF6, which is still −10. Convert your value at 16-bit here, then re-enter the decimal at 32-bit to see the extended pattern.
▸Is my data uploaded?
No. All conversion happens locally in your browser.