In this track: ABI encoding foundations
advanced · 4 MIN READ

Encode signed integers

Negative values use two’s-complement and sign extension.

LET’S PICTURE IT

A fixed-width signed notation needs a rule for carrying the negative sign into a larger word.

SEE IT FOR YOURSELF
Build a transfer call from typed fieldsStep 1 / 4
Build a transfer call from typed fieldstransfer(address,uint256)Recipient: 0x0000…0001Raw amount: 100Diagram labels abbreviate addresses; the lab shows full bytes4-byte selectora9059cbbEach box is one byte; this prefix is not a 32-byte wordWord 0 · recipient address12 zero bytes20 address bytes000…0001Left padding preserves the lower-order address valueSelector4 bytesa9059cbbAddress32 bytesword 0Amount32 bytesword 1 → 0x644 + 32 + 32 = 68 bytesThe integer does not contain token decimals or market price

The standard transfer interface takes an address and a raw integer amount. This diagram uses address one and amount one hundred.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Start with the declared range

int8 supports −128 through 127, while int256 has a much larger signed range. A value outside the declared range should be rejected rather than silently reinterpreted.

Use two’s-complement bits

For a negative number, standard ABI encoding extends the sign into the thirty-two-byte word. The encoding of −1 therefore has every byte set to ff.

The schema changes interpretation

The same all-ff word read as uint256 is a very large positive number. Raw bits do not tell a decoder whether the intended type was signed or unsigned.

Test a boundary

Compare int8 values −1 and 127 in a typed encoder. The first sign-extends; the positive value is zero-padded. Do not infer sign from a display name alone.

Follow the bytes

types: int8
values: ["-1"]
no function selector prefix

word 0 · argument offset 0 bytes
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

A correct decoder needs the int8 schema, not merely the word length.

TURN UNDERSTANDING INTO PRACTICEOpen the ABI encoder →
QUICK CHECK · NO PRESSURE

What does int256(−1) encode as?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.