Problem:
How do we write a negative number (like -1, -2, etc.) in hex?
💡 Simple Answer:
We use something called two's complement, which means:
Instead of storing a "minus" sign, we store a big number that, when read the right way, means a negative value.
An 8-bit number has 8 "slots", so it can hold values from:
Now look at this:
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 00000000 | 0x00 |
| -1 | 11111111 | 0xFF |
| -2 | 11111110 | 0xFE |
| -3 | 11111101 | 0xFD |
| -128 | 10000000 | 0x80 |
So:
0xFF0xFEEven though 0xFF looks like 255, computers know (based on context) that in signed 8-bit, it actually means -1.
If you want to find the hex for a negative number:
For example:
256 + (-1) = 255 → 0xFF4294967296 + (-1) = 4294967295 → 0xFFFFFFFFHere’s how the CPU interprets the same value differently based on context:
; Assembly Code (x86, NASM syntax)
section .data
a db 0xFF ; one byte: 255 or -1 depending on context
section .text
global _start
_start:
movzx eax, byte [a] ; zero-extend: treat a as unsigned
; EAX now = 255
movsx ebx, byte [a] ; sign-extend: treat a as signed
; EBX now = -1
; exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status 0
syscall
0xFF is stored in memory.movzx treats it as unsigned, so you get 255.movsx treats it as signed, so you get -1.The same byte (0xFF) gives different results depending on the instruction used.