How two's complement works
Two's complement stores negative integers by giving the top bit a negative weight equal to −2ⁿ⁻¹, where n is the bit width. Every other bit keeps its normal positive weight. The system lets hardware add positive and negative numbers with the same circuit, which is why nearly all modern processors use it.
In an 8-bit byte the top bit is worth −128 instead of +128. The remaining 7 bits still represent 0 to 127. Add those two parts and the byte covers −128 to 127.
- 0x00 = 0. No bits set.
- 0x7F = 127. All positive bits set, sign bit clear.
- 0x80 = −128. Only the sign bit is set.
- 0xFF = −128 + 127 = −1. Every bit is set.
A hex digit from 8 to F in the top position means the sign bit is 1 and the value is negative. A digit from 0 to 7 means the sign bit is 0 and the value is positive or zero. Flip the bits in the picture to see the sum change.
How to convert signed hex to decimal
Check the sign bit first. If it is 0, convert as a normal unsigned hex number. If it is 1, subtract 2ⁿ from the unsigned result, or invert the bits and add 1.
- Fix the bit width. Each width has a set number of hex digits: 2 for 8 bits, 4 for 16 bits, 8 for 32 bits and 16 for 64 bits.
- Pad the hex value with leading zeros until it fills the width. FF in 16 bits becomes 00FF, which has a 0 sign bit and reads as 255, not −1.
- Check the first hex digit. Digits 8 through F mean the sign bit is 1.
- For a positive value (sign bit 0), convert hex to decimal as normal.
- For a negative value (sign bit 1), use either of the two methods below.
Method 1: subtract 2ⁿ
Convert the hex to its unsigned decimal value and subtract 2ⁿ, where n is the bit width.
FF9C in 16 bits
FF9C unsigned = 65,436
Sign bit is 1 → negative
65,436 − 2¹⁶ = 65,436 − 65,536
Signed value = −100Method 2: invert and add 1
Flip every bit (bitwise NOT), add 1 to the result, convert that to decimal and place a minus sign in front.
FF9C in 16 bits
FF9C in binary = 1111 1111 1001 1100
Invert all bits = 0000 0000 0110 0011 (0x0063)
Add 1 = 0000 0000 0110 0100 (0x0064)
0x0064 = 100
Signed value = −100Both methods give the same answer. Method 1 is faster for mental arithmetic. Method 2 shows the relationship between a negative number and its positive counterpart.
More examples
0x80 in 8 bits
80 unsigned = 128
128 − 2⁸ = 128 − 256
Signed = −128 (the minimum 8-bit signed value)
0xFFFFFFFE in 32 bits
FFFFFFFE unsigned = 4,294,967,294
4,294,967,294 − 2³² = 4,294,967,294 − 4,294,967,296
Signed = −2
0x7FFF in 16 bits
7FFF: first digit is 7, sign bit is 0
Convert as unsigned: 32,767
Signed = 32,767 (the maximum 16-bit signed value)Signed and unsigned ranges
The sign bit splits the range in half. Unsigned values run from 0 to 2ⁿ − 1. Signed values run from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, which shifts the upper half of the unsigned range into negative numbers.
The minimum is always one larger in absolute value than the maximum: 8-bit signed holds −128 but only +127. This asymmetry exists because zero takes one slot from the positive side.
| Width | Hex digits | Signed min | Signed max | Unsigned max |
|---|---|---|---|---|
| 8-bit | 2 | −128 | 127 | 255 |
| 16-bit | 4 | −32,768 | 32,767 | 65,535 |
| 32-bit | 8 | −2,147,483,648 | 2,147,483,647 | 4,294,967,295 |
| 64-bit | 16 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,615 |
Sign extension
Sign extension widens a signed value to a larger bit width by copying the sign bit into every new upper bit. The decimal value stays the same while the hex representation changes.
FF as an 8-bit signed value is −1. To store −1 in 16 bits, copy the sign bit (1) into the 8 new upper bits: FF becomes FFFF. In 32 bits it becomes FFFFFFFF. All three hex strings mean −1 at their respective widths. A positive value extends with zeros: 7F (8-bit, +127) becomes 007F in 16 bits and 0000007F in 32 bits.
Zero extension fills the upper bits with zeros regardless of the sign bit. It treats the original bits as unsigned. FF zero-extended to 16 bits is 00FF (255), not FFFF (−1). Mixing up sign extension and zero extension is a common source of bugs when you cast between integer types in C and C++.
| Value | 8-bit | 16-bit | 32-bit |
|---|---|---|---|
| −1 | FF | FFFF | FFFFFFFF |
| −100 | 9C | FF9C | FFFFFF9C |
| −128 | 80 | FF80 | FFFFFF80 |
| +1 | 01 | 0001 | 00000001 |
| +127 | 7F | 007F | 0000007F |
Integer overflow and wraparound
Adding 1 to the largest positive signed value wraps around to the most negative value. The hex increments normally, but the signed interpretation jumps from the positive extreme to the negative extreme.
8-bit: 7F + 01 = 80 (+127 + 1 = −128)
16-bit: 7FFF + 0001 = 8000 (+32,767 + 1 = −32,768)
32-bit: 7FFFFFFF + 1 = 80000000 (+2,147,483,647 + 1 = −2,147,483,648)The reverse also happens: subtracting 1 from the most negative value wraps to the most positive value. 0x80 − 1 = 0x7F in 8 bits, which jumps from −128 to +127.
C and C++ leave signed overflow undefined, so the compiler may optimise it away or produce unexpected results. Java wraps silently. Python integers have no fixed width, so they never overflow.
Common signed hex values
These hex values appear often in debugging, error handling and system programming. Pick one to see its bits and both readings.
| Hex | Width | Signed | Where it appears |
|---|---|---|---|
FF | 8-bit | −1 | General error return in byte-sized APIs |
80 | 8-bit | −128 | Minimum int8 value |
7F | 8-bit | 127 | Maximum int8 value |
FFFF | 16-bit | −1 | Error return in 16-bit APIs |
8000 | 16-bit | −32,768 | Minimum int16, audio sample floor |
FFFFFFFF | 32-bit | −1 | INVALID_HANDLE_VALUE in Windows, (size_t)−1 |
80000000 | 32-bit | −2,147,483,648 | Minimum int32 (INT_MIN in C) |
7FFFFFFF | 32-bit | 2,147,483,647 | Maximum int32 (INT_MAX in C) |
DEADBEEF | 32-bit | −559,038,737 | Debug fill pattern for freed memory |
CCCCCCCC | 32-bit | −858,993,460 | MSVC debug fill for uninitialised stack |
Signed hex in programming
Converting hex to a signed integer needs different code than unsigned conversion, because most hex-parsing functions return unsigned or arbitrary-precision values by default. Type a hex value: the width follows the number of digits.
Python
Python integers have unlimited width, so int("FF", 16) returns 255, not −1. Use bit arithmetic to apply a fixed width.
def signed(hex_str, bits):
n = int(hex_str, 16)
if n >= (1 << (bits - 1)):
n -= (1 << bits)
return n
signed("FF9C", 16) # -100
signed("FF", 8) # -1
signed("FF", 16) # 255 (00FF in 16 bits, sign bit is 0)JavaScript
parseInt returns a standard number. For 32-bit signed conversion, write the value into an Int32Array, or use a bitwise OR with 0, which forces 32-bit signed interpretation.
(parseInt("FFFFFFFF", 16) | 0) // -1
new Int32Array([0xFFFFFFFF])[0] // -1
new Int8Array([0xFF])[0] // -1C and C++
strtol and std::stoi return signed types by default, but only within the range of their type. For explicit control, cast after parsing.
int32_t n = (int32_t)strtoul("FFFFFFFE", NULL, 16); // -2
int16_t m = (int16_t)strtoul("FF9C", NULL, 16); // -100Java
Integer.parseUnsignedInt (Java 8 and later) parses the full 32-bit range, and a cast to int gives the signed value.
(int) Integer.parseUnsignedInt("FFFFFFFF", 16) // -1
(short) Integer.parseInt("FF9C", 16) // -100| Language | Signed 32-bit from hex | Key detail |
|---|---|---|
| Python | int(h, 16) - (1 << 32) if the top bit is set | Integers have no fixed width |
| JavaScript | parseInt(h, 16) | 0 | Bitwise OR forces int32 |
| C / C++ | (int32_t)strtoul(h, NULL, 16) | Cast after the unsigned parse |
| Java | (int) Integer.parseUnsignedInt(h, 16) | parseInt rejects values above 7FFFFFFF |
Frequently asked questions
What is the difference between signed and unsigned hex?
Why does bit width matter for signed hex?
How do you convert negative decimal to hex?
What is 0xFFFFFFFF as a signed value?
Does two's complement work in all programming languages?
How do you convert a signed 64-bit hex value?
int(h, 16) - (1 << 64) for negative values.What is integer overflow?
Is one's complement the same as two's complement?
Related tools
- Hex to decimal converter: convert hex to decimal with digit-by-digit steps.
- Hex to binary converter: see the full bit pattern of any hex value.
- Hex to float converter: decode IEEE 754 floating-point hex.
- Little-endian hex to decimal: convert byte-swapped hex values.