1 << 31 is undefined behaviour on a signed int in the actor and iterator bit tables #4

Open
opened 2026-08-02 18:32:44 -04:00 by tachikoma · 0 comments
Collaborator

Source: TODO.md, "Internal consistency", item 22 (at bbb7b8f)

AKGL_ACTOR_STATE_UNDEFINED_31 (include/akgl/actor.h:79) and
AKGL_ITERATOR_OP_UNDEFINED_31 (include/akgl/iterator.h:62) are both
(1 << 31). Shifting a 1 into the sign bit of a signed int is undefined
behaviour in C99, not merely implementation-defined.

The two tables do not want the same answer, which is why this is a decision
rather than a one-character fix:

  • akgl_Actor::state is int32_t
  • akgl_Iterator::flags is uint32_t

So an unsigned shift is obviously right for the iterator and changes the type of
every actor state constant if applied there. Worth deciding deliberately rather
than sneaking a u in.

The rest of item 22 -- AKGL_ITERATOR_OP_UPDATE spelled (1 << 0) like its 31
siblings, every 1 << n parenthesised, the hand-aligned value columns preserved,
and the section headings that say each bit pattern is drawn within its own 16-bit
half -- is done. This is what was newly recorded when that work was finished.

Files: include/akgl/actor.h:79, include/akgl/iterator.h:62


Filed by Tachikoma (Claude Code, Opus 5, 1M context)

**Source:** TODO.md, "Internal consistency", item 22 (at bbb7b8f) `AKGL_ACTOR_STATE_UNDEFINED_31` (`include/akgl/actor.h:79`) and `AKGL_ITERATOR_OP_UNDEFINED_31` (`include/akgl/iterator.h:62`) are both `(1 << 31)`. Shifting a 1 into the sign bit of a signed `int` is undefined behaviour in C99, not merely implementation-defined. The two tables do not want the same answer, which is why this is a decision rather than a one-character fix: - `akgl_Actor::state` is `int32_t` - `akgl_Iterator::flags` is `uint32_t` So an unsigned shift is obviously right for the iterator and changes the type of every actor state constant if applied there. Worth deciding deliberately rather than sneaking a `u` in. The rest of item 22 -- `AKGL_ITERATOR_OP_UPDATE` spelled `(1 << 0)` like its 31 siblings, every `1 << n` parenthesised, the hand-aligned value columns preserved, and the section headings that say each bit pattern is drawn within its own 16-bit half -- is done. This is what was newly recorded when that work was finished. **Files:** `include/akgl/actor.h:79`, `include/akgl/iterator.h:62` --- Filed by Tachikoma (Claude Code, Opus 5, 1M context)
tachikoma added this to the 0.9.x milestone 2026-08-02 18:32:44 -04:00
tachikoma added the defecthygieneblast-radius:low labels 2026-08-02 18:32:44 -04:00
tachikoma added the status::grooming label 2026-08-02 18:49:09 -04:00
Sign in to join this conversation.