liveBook · Manning

Binary representation

As explained in Modern C, Third Edition

A binary representation is an abstract, platform-specific model of the values of a C type that determines the results of operations on those values.

An unsigned value

Consider `unsigned x = 240U;`. On a platform where `unsigned` has 16 bits of precision, its binary representation has bits 4, 5, 6, and 7 set; all other bits are zero. The value remains 240 regardless of how the object is stored in memory. section 5.7.1 section 5.7

A type’s binary representation is a platform-specific model of its possible values. Together with operand values, it determines the results of operations. It is observable through properties supplied to C programs, but it is not the object representation: the latter concerns how a value is stored. section 5.1.3 section 5.7

(5.1)
\[\sum_{i = 0}^{p-1}b_{i}2^{i}\]

For a non-negative integer with precision p, each bit bᵢ contributes either zero or 2ⁱ to the value. The equation gives the value represented by those bits, not a layout of the object in memory. section 5.7.1

For an unsigned type, p bits represent values from 0 through 2ᵖ−1. Its maximum is available through a type-specific macro, such as `UINT_MAX` in `<limits.h>`; width macros such as `UINT_WIDTH` expose the corresponding width. These properties let a program account for its platform rather than assume a fixed width. section 5.7.1

TAKEAWAY: A literal’s spelling is not its effective value. Floating-point literals may be rounded to the representable value; even literals that look different can denote the same value. Value, type, and binary representation must be considered together when reasoning about a computation. section 5.3 section 5.8

How Modern C, Third Edition covers it

How deep it goes
Defines the model, works through unsigned bit values and platform limits, and distinguishes it from in-memory object representation.
Examples
Uses a 16-bit unsigned representation of 240 and decimal and hexadecimal floating-point literals near 0.2.
What you can do after
Evaluate/understand operation results using type limits and widths without treating binary representation as a memory layout.
How it connects
Connects binary representation to types, operand values, object representation, unsigned integers, and floating-point literals.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.