Bits, Bytes and Integer

Updated: 29 Nov 2026
Bits, Bytes and Integer

Introduction

This blog post is a summary of CSAPP Chapter 2, covering fundamental concepts of how computers represent and manipulate data.

Computers represent all information as sequences of bits (0 or 1). A byte consists of 8 bits and serves as the basic unit of storage. Unsigned integers use standard binary encoding for non-negative values, while signed integers use two’s complement representation to encode both positive and negative values.

Boolean Algebra

Boolean algebra provides the mathematical foundation for bitwise operations.

  • AND: p∧q=1p \land q = 1 if and only if both p=1p = 1 and q=1q = 1
  • OR: p∨q=1p \lor q = 1 if at least one of p=1p = 1 or q=1q = 1
  • NOT: ¬p=1\neg p = 1 if p=0p = 0, and ¬p=0\neg p = 0 if p=1p = 1

These operations form the basis for all logical computations in digital systems.

De Morgan’s Laws

De Morgan’s laws describe the relationship between negation and the logical operations AND and OR. These laws allow to transform expressions by distributing negation over conjunctions and disjunctions.

¬(p∨q)=(¬p)∧(¬q)¬(p∧q)=(¬p)∨(¬q)\begin{align} \neg(p \lor q) &= (\neg p) \land (\neg q) \\ \neg(p \land q) &= (\neg p) \lor (\neg q) \end{align}

Wiki: De Morgan’s laws.

XOR

XOR Exclusive OR returns 1 when exactly one of the inputs is 1. XOR can be expressed using the fundamental operations:

p⊕q=(p∧¬q)∨(¬p∧q)\begin{align} p \oplus q = (p \land \neg q) \lor (\neg p \land q) \end{align}

Using De Morgan’s laws, it is possible to express XOR using only NOT and AND operations. Starting from the XOR definition:

p⊕q=(p∧¬q)∨(¬p∧q)¬(p⊕q)=¬(p∧¬q)∧¬(¬p∧q)¬¬(p⊕q)=¬(¬(p∧¬q)∧¬(¬p∧q))p⊕q=¬(¬(p∧¬q)∧¬(¬p∧q))\begin{align} p \oplus q &= (p \land \neg q) \lor (\neg p \land q) \\ \neg(p \oplus q) &= \neg\left(p \land \neg q\right) \land \neg\left(\neg p \land q\right) \\ \neg\neg(p \oplus q) &= \neg\left(\neg\left(p \land \neg q\right) \land \neg\left(\neg p \land q\right)\right) \\ p \oplus q &= \neg\left(\neg\left(p \land \neg q\right) \land \neg\left(\neg p \land q\right)\right) \end{align}

The final expression uses only NOT and AND operations.

Branchless Conditionals

Conditional selection can be implemented without branching using bitwise operations. The expression cond ? x : y can be computed as:

(cond∧x)∨(¬cond∧y)(\text{cond} \land x) \lor (\neg \text{cond} \land y)

Boolean logic perspective: When cond=1\text{cond} = 1 (true), the first term evaluates to xx and the second term evaluates to 00, resulting in xx. When cond=0\text{cond} = 0 (false), the first term evaluates to 00 and the second term evaluates to yy, resulting in yy.

Mathematical perspective: The expression can be viewed as a linear combination: when cond\text{cond} is a mask (all bits set to 1 or all bits set to 0), it acts as a selector. If cond\text{cond} is all 1s, then cond∧x=x\text{cond} \land x = x and ¬cond∧y=0\neg \text{cond} \land y = 0, yielding xx. If cond\text{cond} is all 0s, then cond∧x=0\text{cond} \land x = 0 and ¬cond∧y=y\neg \text{cond} \land y = y, yielding yy.

Important: The condition cond\text{cond} must be a mask (all bits 0 or all bits 1), not just a boolean value. This ensures that bitwise operations work correctly across all bits of the operands.

Integer Representation

Unsigned Encodings

Unsigned integers represent non-negative values using standard binary encoding. For a ww-bit binary vector, the conversion to unsigned integer is defined as follows.

For vector x⃗=[xw−1,xw−2,…,x0]\vec{x} = [x_{w-1}, x_{w-2}, \dots, x_0]:

B2Uw(x⃗)≐∑i=0w−1xi2iB2U_w(\vec{x}) \doteq \sum_{i=0}^{w-1} x_i 2^i

The maximum value for a ww-bit unsigned integer is:

UMaxw=2w−1\text{UMax}_w = 2^w - 1

Two’s Complement Encodings

Two’s complement encoding is the most common representation for signed integers. The most significant bit xw−1x_{w-1} (the sign bit) has negative weight −2w−1-2^{w-1}.

For vector x⃗=[xw−1,xw−2,…,x0]\vec{x} = [x_{w-1}, x_{w-2}, \dots, x_0]:

B2Tw(x⃗)≐−xw−12w−1+∑i=0w−2xi2iB2T_w(\vec{x}) \doteq -x_{w-1}2^{w-1} + \sum_{i=0}^{w-2} x_i 2^i

The sign bit xw−1x_{w-1} determines the sign: if xw−1=1x_{w-1} = 1, the value is negative; if xw−1=0x_{w-1} = 0, the value is non-negative.

The range of values for a ww-bit two’s-complement integer is:

TMinw=−2w−1TMaxw=2w−1−1\begin{align} \text{TMin}_w &= -2^{w-1} \\ \text{TMax}_w &= 2^{w-1} - 1 \end{align}

Two’s-complement representation exhibits an asymmetric property: the absolute value of TMaxw\text{TMax}_w plus one equals the absolute value of TMinw\text{TMin}_w:

∣TMaxw∣+1=∣TMinw∣=2w−1|\text{TMax}_w| + 1 = |\text{TMin}_w| = 2^{w-1}

This asymmetry arises because TMaxw+1=2w−1\text{TMax}_w + 1 = 2^{w-1} while TMinw=−2w−1\text{TMin}_w = -2^{w-1}, meaning there is one more negative number than positive numbers in the representable range.

Conversions between Signed and Unsigned

The same bit pattern can be interpreted as either signed or unsigned, with different numeric values. Conversion functions map between these interpretations.

Two’s-complement to Unsigned (T2U):

For xx such that TMinw≤x≤TMaxw\text{TMin}_w \leq x \leq \text{TMax}_w:

T2Uw(x)={x+2w,if x<0x,if x≥0T2U_w(x) = \begin{cases} x + 2^w, & \text{if } x < 0 \\ x, & \text{if } x \geq 0 \end{cases}

Notable mappings: T2Uw(−1)=UMaxwT2U_w(-1) = \text{UMax}_w and T2Uw(TMinw)=2w−1T2U_w(\text{TMin}_w) = 2^{w-1}.

Unsigned to Two’s-complement (U2T):

For uu such that 0≤u≤UMaxw0 \leq u \leq \text{UMax}_w:

U2Tw(u)={u,if u≤TMaxwu−2w,if u>TMaxwU2T_w(u) = \begin{cases} u, & \text{if } u \leq \text{TMax}_w \\ u - 2^w, & \text{if } u > \text{TMax}_w \end{cases}

Notable mapping: U2Tw(UMaxw)=−1U2T_w(\text{UMax}_w) = -1.

Integer Operation

Addition

Unsigned

Unsigned addition follows modular arithmetic, wrapping around when the sum exceeds the maximum representable value. For xx and yy such that 0≤x,y<2w0 \leq x, y < 2^w:

x+wuy={x+y,if x+y<2wx+y−2w,if 2w≤x+y<2w+1NormalOverflowx +_w^u y = \begin{cases} x + y, & \text{if } x + y < 2^w \\ x + y - 2^w, & \text{if } 2^w \leq x + y < 2^{w+1} \end{cases} \quad \begin{array}{l} \text{Normal} \\ \text{Overflow} \end{array}

Overflow behavior: When overflow occurs (x+y≥2wx + y \geq 2^w), the result wraps around by subtracting 2w2^w, effectively mapping values towards zero. This is equivalent to modular arithmetic: x+wuy=(x+y) mod 2wx +_w^u y = (x + y) \bmod 2^w.

Two’s-complement

Two’s-complement addition handles both positive and negative overflow cases. For integer values xx and yy in the range −2w−1≤x,y≤2w−1−1-2^{w-1} \leq x, y \leq 2^{w-1} - 1:

x+wty={x+y−2w,if 2w−1≤x+yx+y,if −2w−1≤x+y<2w−1x+y+2w,if x+y<−2w−1Positive overflowNormalNegative overflowx +_w^t y = \begin{cases} x + y - 2^w, & \text{if } 2^{w-1} \leq x + y \\ x + y, & \text{if } -2^{w-1} \leq x + y < 2^{w-1} \\ x + y + 2^w, & \text{if } x + y < -2^{w-1} \end{cases} \quad \begin{array}{l} \text{Positive overflow} \\ \text{Normal} \\ \text{Negative overflow} \end{array}

Overflow behavior:

  • Positive overflow: When x+y≥2w−1x + y \geq 2^{w-1}, the result wraps around by subtracting 2w2^w, mapping to negative values. Specifically, when x+y=2w−1x + y = 2^{w-1} (which is TMaxw+1\text{TMax}_w + 1), the result becomes −2w−1=TMinw-2^{w-1} = \text{TMin}_w.
  • Negative overflow: When x+y<−2w−1x + y < -2^{w-1}, the result wraps around by adding 2w2^w, mapping to positive values. Specifically, when x+y=−2w−1−1x + y = -2^{w-1} - 1 (which is TMinw−1\text{TMin}_w - 1), the result becomes 2w−1−1=TMaxw2^{w-1} - 1 = \text{TMax}_w.

Multiplication

Multiplication follows modular arithmetic, truncating the 2w2w bit product to ww bits. For unsigned multiplication, the result is simply the product modulo 2w2^w:

x∗wuy=(x⋅y) mod 2wx *_w^u y = (x \cdot y) \bmod 2^w

For two’s complement multiplication, the process involves computing the unsigned product modulo 2w2^w and then converting it to two’s-complement representation. For xx and yy such that TMinw≤x,y≤TMaxw\text{TMin}_w \leq x, y \leq \text{TMax}_w:

x∗wty=U2Tw((x⋅y) mod 2w)x *_w^t y = U2T_w((x \cdot y) \bmod 2^w)

Bit-level equivalence: The bit-level representation of the ww-bit two’s-complement product is identical to the bit-level representation of the ww-bit unsigned product. This means that at the hardware level, the same multiplication operation works for both signed and unsigned integers only the interpretation of the result differs.

Float Point Number

IEEE 754 floating-point representation encodes real numbers using three components: sign bit, exponent field, and fraction field. The format supports single-precision (32-bit) and double-precision (64-bit) representations.

IEEE 754 Floating-Point Bit Structure

Figure: IEEE 754 single-precision (32-bit) and double-precision (64-bit) bit layouts. Source: CSAPP Chapter 2.

Floating-Point Value Cases

Figure: Four cases of floating-point representation: Normalized, Denormalized, Infinity, and NaN. Source: CSAPP Chapter 2.

Normalized Values

Normalized values are the most common case, occurring when the exponent field is neither all zeros nor all ones. The exponent field is interpreted as a biased signed integer.

For a kk-bit exponent field, the bias is:

Bias=2k−1−1\text{Bias} = 2^{k-1} - 1

The exponent value EE is computed as:

E=e−BiasE = e - \text{Bias}

where ee is the unsigned number represented by the exponent bits. For single precision (k=8k = 8), the bias is 127, yielding exponent range [−126,+127][-126, +127]. For double precision (k=11k = 11), the bias is 1023, yielding exponent range [−1022,+1023][-1022, +1023].

The fraction field ff represents a fractional value in the range 0≤f<10 \leq f < 1, with binary representation 0.fn−1…f1f00.f_{n-1} \ldots f_1 f_0. The value of a normalized floating-point number is:

V=(−1)s×M×2EV = (-1)^s \times M \times 2^E

where ss is the sign bit, M=1+fM = 1 + f is the significand (mantissa), and EE is the exponent. This kind of implicit way to add one more bit for precision.

Denormalized Values

Denormalized values occur when the exponent field is all zeros. Unlike normalized values, denormalized numbers do not have an implied leading 1 in the significand.

The exponent value is:

E=1−BiasE = 1 - \text{Bias}

The significand is:

M=fM = f

where ff is the value of the fraction field without an implied leading 1. This allows representation of zero and very small numbers that cannot be represented in normalized form. The value of a denormalized floating-point number is:

V=(−1)s×M×2EV = (-1)^s \times M \times 2^E

Special cases: The bit pattern with all zeros (sign bit = 0, exponent = 0, fraction = 0) represents +0.0+0.0. When the sign bit is 1 with all other fields zero, it represents −0.0-0.0. In IEEE floating-point, +0.0+0.0 and −0.0-0.0 are considered equal in comparisons but may behave differently in some operations.

References

Bryant, R. E., & O’Hallaron, D. R. (2016). Computer Systems: A Programmer’s Perspective, Third Edition. Pearson. https://csapp.cs.cmu.edu/

CMU 15-213: Introduction to Computer Systems. Carnegie Mellon University. https://www.cs.cmu.edu/~213/

De Morgan’s laws. (2025, October 18). In Wikipedia. https://en.wikipedia.org/wiki/De_Morgan%27s_laws

Suggest an edit

Last modified: 29 Nov 2026