liveBook · Manning

Bit shifting

As explained in Modern C

A shift operation is a C bitwise operation that moves the bits of an integer representation left or right by a specified operand.

In C, a shift operation moves an integer’s bits left or right by the right operand. For unsigned values, left shifting corresponds to multiplying by a power of two; right shifting corresponds to integer division. Bits that leave the representation are discarded. section 5.7.3

Shifting 240

Take A = 240, whose set bits occupy positions 4 through 7. A << 2 produces 960, with bits in positions 6 through 9. A << 9 would require bit 16 for the full result, so the representation retains only positions 13 through 15 and produces 57344. The right operand must be less than the type’s precision. The related operators <<= and >>= combine shifting with assignment. section 5.7.3

Use shifts deliberately when treating unsigned values as bit sets:

  • Use << to specify powers of two and construct individual bit masks. section 5.7.3
  • Use >> to discard less-significant bits; the discarded portion corresponds to the remainder modulo 1ULL << n. section 5.7.3
  • Prefer unsigned types for bit operations because shifts of negative signed values have unclear semantics. section 5.7.5
#define FLOCK_MAGPIE (1U << magpie)
#define FLOCK_RAVEN    (1U << raven)
#define FLOCK_JAY      (1U << jay)
#define FLOCK_CHOUGH   (1U << chough)
#define FLOCK_EMPTY     0U
#define FLOCK_FULL    ((1U << corvid_num)-1)


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":7}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":7}]]"}
!@%STYLE%@!

Inspect mask definitions that use 1U << enumeration values to represent flags and construct empty or full sets. section 5.7.3

How Modern C covers it

How deep it goes
Explains bit movement, arithmetic correspondence, discarded bits, precision constraints, and signedness, without resolving negative signed shifts.
Examples
Works through A = 240, overflowed left shifts, right-shift remainders, and named enumeration masks.
What it assumes
Assumes familiarity with integer values and binary positions while defining precision, bit sets, and signedness in context.
What you can do after
Apply it via C shift operators to calculate powers of two, manipulate bit sets, and define flag masks.
How it connects
Connects unsigned integers, bit sets, powers of two, integer division, enumeration masks, and signed integer representation.

Also explained in

Compare how 2 books explain bit shifting →

Related concepts

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