Skip to content
BIASED

Bits, integers and byte order

Block 1 of 6~2 min

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.

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