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 -bit vector is , with most significant. is the unsigned reading, the two’s-complement reading. is bitwise complement. Subscripts mark bases: , 0x2C.
A bit is two voltage levels far enough apart that noise rarely flips them. As an unsigned integer,
bits give patterns mapped onto . 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: = 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 -bit adder keeps only bits:
In 4 bits, and the adder returns : wraps to . 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 and to the unsigned adder in 4 bits: , not zero. Sign-and-magnitude survives in the float sign bit; integers use something better.
Key idea. Give the top bit weight instead of :
so . The same adder serves both readings. Range: (one more negative than positive). For 8 bits that is ; for 32 bits about ; for 64 bits about . All-ones is ; the pattern 1000…0 is the most negative number.
Worked example. 0xD6 : unsigned 214; signed (also ).
Because with no carries, negation is . Subtraction is . Edge cases: (one zero); in 8 bits, wraps back to because that magnitude has no positive partner.
Overflow and extension
Hardware never “errors” on a -bit wrap; it returns the low bits.
- Unsigned overflow: true sum (carry out of the top).
- Signed overflow: true sum outside — same-sign operands, other-sign result. Opposite signs never overflow.
In 4 bits: is unsigned 8 (ok) and signed (overflow); is unsigned wrap and signed (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 . Four-bit → sext 0xFB (), zext . RISC-V: lb/lh vs lbu/lhu. Truncation keeps the low bits (value mod ): 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 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: . 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 mod ; logical right fills zeros (unsigned ); arithmetic right copies the sign bit ( toward ). For (): arithmetic >>2 → ; C’s -118/4 is 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 to , via IEEE 754.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.