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: if and only if both and
- OR: if at least one of or
- NOT: if , and if
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.
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:
Using De Morgan’s laws, it is possible to express XOR using only NOT and AND operations. Starting from the XOR definition:
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:
Boolean logic perspective: When (true), the first term evaluates to and the second term evaluates to , resulting in . When (false), the first term evaluates to and the second term evaluates to , resulting in .
Mathematical perspective: The expression can be viewed as a linear combination: when is a mask (all bits set to 1 or all bits set to 0), it acts as a selector. If is all 1s, then and , yielding . If is all 0s, then and , yielding .
Important: The condition 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 -bit binary vector, the conversion to unsigned integer is defined as follows.
For vector :
The maximum value for a -bit unsigned integer is:
Two’s Complement Encodings
Two’s complement encoding is the most common representation for signed integers. The most significant bit (the sign bit) has negative weight .
For vector :
The sign bit determines the sign: if , the value is negative; if , the value is non-negative.
The range of values for a -bit two’s-complement integer is:
Two’s-complement representation exhibits an asymmetric property: the absolute value of plus one equals the absolute value of :
This asymmetry arises because while , 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 such that :
Notable mappings: and .
Unsigned to Two’s-complement (U2T):
For such that :
Notable mapping: .
Integer Operation
Addition
Unsigned
Unsigned addition follows modular arithmetic, wrapping around when the sum exceeds the maximum representable value. For and such that :
Overflow behavior: When overflow occurs (), the result wraps around by subtracting , effectively mapping values towards zero. This is equivalent to modular arithmetic: .
Two’s-complement
Two’s-complement addition handles both positive and negative overflow cases. For integer values and in the range :
Overflow behavior:
- Positive overflow: When , the result wraps around by subtracting , mapping to negative values. Specifically, when (which is ), the result becomes .
- Negative overflow: When , the result wraps around by adding , mapping to positive values. Specifically, when (which is ), the result becomes .
Multiplication
Multiplication follows modular arithmetic, truncating the bit product to bits. For unsigned multiplication, the result is simply the product modulo :
For two’s complement multiplication, the process involves computing the unsigned product modulo and then converting it to two’s-complement representation. For and such that :
Bit-level equivalence: The bit-level representation of the -bit two’s-complement product is identical to the bit-level representation of the -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.

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

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 -bit exponent field, the bias is:
The exponent value is computed as:
where is the unsigned number represented by the exponent bits. For single precision (), the bias is 127, yielding exponent range . For double precision (), the bias is 1023, yielding exponent range .
The fraction field represents a fractional value in the range , with binary representation . The value of a normalized floating-point number is:
where is the sign bit, is the significand (mantissa), and 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:
The significand is:
where 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:
Special cases: The bit pattern with all zeros (sign bit = 0, exponent = 0, fraction = 0) represents . When the sign bit is 1 with all other fields zero, it represents . In IEEE floating-point, and 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
Last modified: 29 Nov 2026