Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 40 ยท Data, Numbers and Registers

Binary, Hex and Two's Complement

Learn bits, bytes and hex, how two's complement stores negative numbers, sign and zero extension, overflow and wraparound, and endianness with runnable examples.

A register has no idea whether it holds a number, a letter, an address or a color. It holds a fixed number of bits, nothing more. Everything else is interpretation: the same 8 bits can mean 200, or -56, or the letter "รˆ", depending on which instruction looks at them. Assembly makes this visible in a way that high-level languages hide, so understanding binary, hexadecimal and two's complement is the foundation for everything that follows.

This lesson covers how bits are grouped, how to read hexadecimal at a glance, how negative numbers are stored, what happens when a result does not fit, and in which order the bytes of a bigger number sit in memory. All the examples run in the editor, using JavaScript's typed arrays and BigInt as stand-ins for fixed-size registers.

Bits, bytes and words

A bit is a 0 or a 1. A byte is 8 bits and can hold 256 different patterns. Processors work on larger groups too, and assembly names them by size:

Size Bits Typical names Unsigned range
Byte 8 byte, BYTE PTR 0 to 255
Word 16 word 0 to 65,535
Doubleword 32 dword, w registers on ARM64 0 to 4,294,967,295
Quadword 64 qword, x registers on ARM64 0 to 18,446,744,073,709,551,615

The names "word" and "doubleword" are historical: they come from 16-bit x86 processors, and x86 assemblers still use them even though modern CPUs are 64-bit. ARM documentation uses "word" for 32 bits too, so always check which meaning is in play.

Each bit position has a weight that doubles as you move left: 1, 2, 4, 8, 16, 32, 64, 128 for a byte. The pattern 0010 1010 has bits set at weights 32, 8 and 2, so it equals 42.

Hexadecimal: binary in short form

Writing 64 ones and zeros is unreadable, so we use hexadecimal (base 16). One hex digit stands for exactly four bits (a nibble), so a byte is two hex digits and a 64-bit register is sixteen. The digits are 0 to 9 and then a to f for 10 to 15.

Binary Hex Binary Hex Binary Hex Binary Hex
0000 0 0100 4 1000 8 1100 c
0001 1 0101 5 1001 9 1101 d
0010 2 0110 6 1010 a 1110 e
0011 3 0111 7 1011 b 1111 f

To convert, translate each digit separately: 0x2a is 0010 1010. Assembly writes hex with a 0x prefix (GAS, ARM and C style) or an h suffix (traditional NASM and MASM, as in 2ah). A few values are worth memorizing: 0xff is 255, 0x7f is 127, 0x80 is 128, 0xffff is 65,535 and 0x7fffffff is the largest signed 32-bit number, 2,147,483,647.

Signed numbers: two's complement

How do you store -5 in a fixed number of bits? Every modern CPU uses two's complement. The rule is simple: the top bit has a negative weight. For 8 bits the weights are -128, 64, 32, 16, 8, 4, 2, 1. The pattern 1111 1011 is -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5.

flowchart LR
    B["Same 8 bits<br/>1100 1000 = 0xc8"] -->|"read as unsigned"| U["200"]
    B -->|"read as signed (two's complement)"| S["-56"]

The bits never change. Only the interpretation does: 200 as an unsigned byte, -56 as a signed byte.

Why this scheme? Because addition and subtraction work identically for signed and unsigned numbers, so the CPU needs only one adder. To negate a number, flip every bit and add 1. The signed range for n bits is -2^(n-1) to 2^(n-1) - 1, which is -128 to 127 for a byte and -2,147,483,648 to 2,147,483,647 for 32 bits. The range has one more negative number than positive, because zero takes one slot on the non-negative side.

Run the program below. It prints the same bit patterns twice, as unsigned and as signed values, and then negates a few numbers by flipping and adding one.

JS One bit pattern, two meanings

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that every pattern with the top bit set is negative when read as signed. That is why the top bit is called the sign bit. It is also why 0xff is both 255 and -1: it is all ones, and adding 1 to it wraps around to zero in either reading.

Overflow and wraparound

A register has a fixed size, so arithmetic is modular: a result that does not fit loses its top bits. Adding 1 to the largest unsigned byte (255) gives 0. Adding 1 to the largest signed byte (127) gives -128. The hardware does the same thing in both cases and records what happened in two flags:

  • The carry flag (CF) is set when an addition carries out of the top bit, or a subtraction needs a borrow. It tells you an unsigned result is wrong.
  • The overflow flag (OF) is set when the signed result has the wrong sign for its operands, for example two positive numbers adding up to a negative one. It tells you a signed result is wrong.

The CPU does not know which interpretation you intended, so it sets both flags every time, and you pick the one that matches your data. Conditional jumps for unsigned comparisons check CF; those for signed comparisons check OF and the sign flag. Flags explains this in depth. The example below adds pairs of bytes and reports both flags.

JS Wraparound, carry and overflow

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the first line: 0x7f + 0x01 is 127 + 1. As unsigned, 128 is correct, so CF is 0. As signed, 128 does not exist, so the result is -128 and OF is 1. The second line is the opposite: 255 + 1 wraps to 0 (unsigned wrong, CF=1), but as signed it is -1 + 1 = 0, which is correct (OF=0). The same four bits of hardware produce both answers.

Sign extension and zero extension

Often a program has to widen a value: a byte read from memory becomes a 32-bit register value. How you fill the new top bits depends on whether the byte is signed:

  • Zero extension fills with zeros. The value stays the same when read as unsigned: 0xf6 (246) becomes 0x000000f6 (246).
  • Sign extension copies the sign bit into all new bits. The value stays the same when read as signed: 0xf6 (-10) becomes 0xfffffff6 (-10).

Choosing the wrong one is a classic bug: a char holding 200 turns into -56 when sign-extended. The last two lines of the previous example show the same byte both ways. In x86-64 assembly the instructions are movzx (zero-extend) and movsx/movsxd (sign-extend); on ARM64 they are the uxtb/uxth and sxtb/sxth/sxtw family. MOV, LEA and Moving Data covers them in detail.

Endianness: which byte comes first

A 32-bit number such as 0x12345678 needs four bytes of memory. Which of the four goes at the lowest address? Little-endian machines store the least significant byte first: 78 56 34 12. Big-endian machines store the most significant byte first: 12 34 56 78. x86-64 is little-endian, and so are ARM64 systems in practice (Linux, Android, Windows, macOS and iOS all run it little-endian). Network protocols use big-endian, so they are often called "network byte order".

flowchart LR
    subgraph L["Little-endian memory"]
        direction LR
        L0["addr 0<br/>78"] --- L1["addr 1<br/>56"] --- L2["addr 2<br/>34"] --- L3["addr 3<br/>12"]
    end
    subgraph B["Big-endian memory"]
        direction LR
        B0["addr 0<br/>12"] --- B1["addr 1<br/>34"] --- B2["addr 2<br/>56"] --- B3["addr 3<br/>78"]
    end

The number 0x12345678 stored in four consecutive bytes, in the two orders.

Endianness only matters when you look at memory byte by byte, or when data crosses a boundary such as a file or a network socket. Inside a register there is no order, only the number. In the editor below, a DataView writes the same 32-bit number both ways, and then the program reinterprets the same four bytes in the wrong order. After that it uses BigInt to model a full 64-bit register wrapping around.

JS Endianness and 64-bit wraparound

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line shows an important identity: -1 as an unsigned 64-bit number is 0xffffffffffffffff. When you see a register full of fs in a debugger, it is probably -1.

Tip: When a number looks "too big" in a debugger, try reading it as signed. When a negative number looks wrong, try reading it as unsigned. Many confusing bugs are just a signed/unsigned mismatch between two instructions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the 8-bit pattern 1111 1111 mean in two's complement?

    All ones is 255 as an unsigned byte and -1 as a signed byte. Adding 1 to it wraps around to 0 in both readings.
  2. 2. Which flag tells you that an unsigned addition produced a wrong result because it did not fit?

    CF is set when an addition carries out of the top bit, meaning the unsigned result wrapped. OF is the flag for signed wrongness.
  3. 3. A signed byte 0xf6 is widened to 32 bits. Which method keeps its value of -10?

    Sign extension copies the top bit into every new bit, so the signed value stays -10. Zero extension would turn it into 246.
  4. 4. How is 0x12345678 stored in memory on a little-endian machine, from the lowest address up?

    Little-endian puts the least significant byte at the lowest address, giving 78 56 34 12. Each byte itself is not reversed.
  5. 5. What is the largest signed 32-bit number?

    With the sign bit clear and all other bits set, 0x7fffffff equals 2,147,483,647. Adding 1 gives 0x80000000, which is the most negative value.

Summary

  • A register holds only bits; signed, unsigned, pointer and character are interpretations chosen by the instructions you use.
  • One hex digit is four bits, so a byte is two hex digits; memorize 0xff, 0x7f, 0x80 and 0x7fffffff.
  • In two's complement the top bit has negative weight; negate by flipping all bits and adding 1; n bits cover -2^(n-1) to 2^(n-1)-1.
  • Arithmetic wraps. CF flags unsigned wrongness, OF flags signed wrongness, and the CPU sets both every time.
  • Zero extension widens unsigned values; sign extension widens signed values (movzx and movsx).
  • x86-64 and practically all ARM64 systems are little-endian: the least significant byte is at the lowest address.

Next: Registers: The CPU's Working Memory, the places where all of these bit patterns live.