Skip to content
BIASED

Bits, integers and byte order

Binary and hex, unsigned integers and two’s complement, overflow, shifts, little-endian and UTF-8 — the interpretations of the same bits the rest of the course relies on.

Bits, bytes and hexadecimal

The previous topic ended with a memory that holds only numbers: the instruction 0x00B50633, the integers 5 and 7, the 13 bytes of "hello, world\n". A cell does not hold “the number 5”; it holds electrical levels. This topic sets out the interpretations every later note relies on: unsigned and two’s-complement integers, overflow, bitwise ops, endianness and text.

Notation. A ww-bit vector is x=xw−1…x0x = x_{w-1}\ldots x_0, with xw−1x_{w-1} most significant. U(x)U(x) is the unsigned reading, T(x)T(x) the two’s-complement reading. xˉ\bar x is bitwise complement. Subscripts mark bases: 101121011_2, 0x2C.

A bit is two voltage levels far enough apart that noise rarely flips them. As an unsigned integer,

U(x)=∑i=0w−1xi 2i,U(1011002)=32+8+4=44.U(x)=\sum_{i=0}^{w-1} x_i\,2^i,\qquad U(101100_2)=32+8+4=44.

ww bits give 2w2^w patterns mapped onto 0,…,2w−10,\ldots,2^w-1. A byte is 8 bits (the smallest addressed unit). A word is the processor’s natural unit: 32 bits in RV32I, 64 in RV64. Group bits by four for hex: 0010 110020010\,1100_2 = 0x2C = 44. A byte is two hex digits; a 32-bit word is eight — which is why addresses and machine words are written in hex.

C does not fix integer widths, only minimum ranges. On LP64 Linux/macOS, char is 1 byte, short 2, int 4, long 8 and pointers 8; on ILP32 RISC-V, long and pointers are 4; on 64-bit Windows, long stays 4. Use <stdint.h> (int8_t, uint32_t, int64_t) when width matters.

Unsigned integers and wraparound

Unsigned addition is the school algorithm in base 2: add column by column, carry 1 when a column sums to 2 or 3. A ww-bit adder keeps only ww bits:

(a+b) mod 2w.(a+b)\bmod 2^w.

In 4 bits, 11012+00112=1 000021101_2+0011_2=1\,0000_2 and the adder returns 00000000: 13+313+3 wraps to 00. Unsigned numbers sit on a circle, not a line — add one and you step clockwise; after all-ones comes zero.

Two’s complement

Baseline. Sign-and-magnitude puts a sign bit in front: two zeros (+0 and −0), and addition of mixed signs needs a separate circuit. Feed +5+5 and −5-5 to the unsigned adder in 4 bits: 0101+1101=0010=+20101+1101=0010=+2, not zero. Sign-and-magnitude survives in the float sign bit; integers use something better.

Key idea. Give the top bit weight −2w−1-2^{w-1} instead of +2w−1+2^{w-1}:

T(x)=−xw−1 2w−1+∑i=0w−2xi 2i=U(x)−xw−1 2w,T(x)=-x_{w-1}\,2^{w-1}+\sum_{i=0}^{w-2}x_i\,2^i=U(x)-x_{w-1}\,2^w,

so T≡U(mod2w)T\equiv U\pmod{2^w}. The same adder serves both readings. Range: −2w−1…2w−1−1-2^{w-1}\ldots 2^{w-1}-1 (one more negative than positive). For 8 bits that is −128…127-128\ldots 127; for 32 bits about ±2.1×109\pm 2.1\times 10^9; for 64 bits about ±9.2×1018\pm 9.2\times 10^{18}. All-ones is −1-1; the pattern 1000…0 is the most negative number.

Worked example. 0xD6 =1101 01102=1101\,0110_2: unsigned 214; signed −128+64+16+4+2=−42-128+64+16+4+2=-42 (also 214−256214-256).

Because x+xˉ=2w−1x+\bar x=2^w-1 with no carries, negation is xˉ+1\bar x+1. Subtraction is a+bˉ+1a+\bar b+1. Edge cases: −0=0-0=0 (one zero); in 8 bits, −(−128)-(-128) wraps back to −128-128 because that magnitude has no positive partner.

Overflow and extension

Hardware never “errors” on a ww-bit wrap; it returns the low bits.

  • Unsigned overflow: true sum ≥2w\ge 2^w (carry out of the top).
  • Signed overflow: true sum outside [−2w−1,2w−1−1][-2^{w-1},2^{w-1}-1] — same-sign operands, other-sign result. Opposite signs never overflow.

In 4 bits: 0111+0001=10000111+0001=1000 is unsigned 8 (ok) and signed −8-8 (overflow); 1101+0011=00001101+0011=0000 is unsigned wrap and signed −3+3=0-3+3=0 (ok). The same bits can overflow in one reading and be correct in the other.

RISC-V add/addi/sub write the low bits and never trap. Unsigned wrap is easy to detect: add t0, t1, t2 then bltu t0, t1, overflow. C: unsigned wraps by definition (UINT_MAX + 1u is 0); signed overflow is undefined behaviour — compilers may assume it never happens (x+1>x for int becomes constantly true at -O2). Mixed comparisons convert the signed operand: -1 < 0u is false. -fsanitize=undefined reports signed overflows at run time.

Extension. Zero-extend preserves unsigned value; sign-extend copies the top bit and preserves TT. Four-bit 10111011 → sext 0xFB (−5-5), zext 1111. RISC-V: lb/lh vs lbu/lhu. Truncation keeps the low kk bits (value mod 2k2^k): 0x00000100 truncated to a byte is 0.

Bitwise operations and shifts

Bitwise &, |, ^, ~ act column by column. Treat a word as flags or packed fields: set bit nn with x|(1<<n); clear with x&~(1<<n); test with (x>>n)&1; extract a field by shifting down and masking. x&(x-1) clears the lowest set bit: 0101 1000 & 0101 0111=0101 00000101\,1000\,\&\,0101\,0111=0101\,0000. Logical &&/||/! treat the whole value as true/false and short-circuit: 0x69 & 0x55 is 0x41, while 0x69 && 0x55 is 1.

Shifts: left <<k multiplies by 2k2^k mod 2w2^w; logical right fills zeros (unsigned ⌊U/2k⌋\lfloor U/2^k\rfloor); arithmetic right copies the sign bit (⌊T/2k⌋\lfloor T/2^k\rfloor toward −∞-\infty). For 1000 101021000\,1010_2 (−118-118): arithmetic >>2 → −30-30; C’s -118/4 is −29-29 toward zero. RISC-V: sll/srl/sra (and immediates); only the low 5 bits of the shift amount count on RV32.

Byte order and text

A 32-bit word occupies four addresses. Little-endian (RISC-V, x86, ARM in practice) stores the least significant byte at the lowest address: 0x12345678 at 0x100 is 78 56 34 12. Big-endian stores 12 34 56 78 — also network byte order (htonl/ntohl). Endianness never reorders array elements: a[0] is still before a[1]. Reading a little-endian word with a byte pointer and rebuilding it needs care; casting through uint32_t * assumes alignment.

Text. ASCII maps 0–127: 'A'=65=0x41, 'a'=97, '0'=48, '\n'=10, '\0'=0. UTF-8 encodes each Unicode code point in 1–4 bytes; ASCII bytes are valid UTF-8 unchanged. 'Ж' (U+0416) is two bytes D0 96; a four-byte emoji occupies four addresses. C’s char is a byte, not a character: walking a UTF-8 string one char at a time can land mid-code-point.

The next topic, Floating-point numbers, covers values that integers cannot: fractions and ranges like 10−3010^{-30} to 103010^{30}, via IEEE 754.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.