Skip to content

✨ (minor) v6: Add hal::bit_is_set() - #129

Merged
kammce merged 1 commit into
mainfrom
v6-bit-is-set
Nov 14, 2026
Merged

kammce merged 1 commit into
mainfrom
v6-bit-is-set

Conversation

@kammce

@kammce kammce commented Nov 13, 2026

Copy link
Copy Markdown
Member

Adds hal::bit_is_set<mask>(value): whether the one bit that a single-bit mask selects is 1, as a bool.

static constexpr auto busy = hal::bit_mask::from<7>();
while (hal::bit_is_set<busy>(reg->status)) {
  continue;
}

Single-bit checks are written today as hal::bit_extract<mask>(value) != 0, as == 1, or as the bare result in a condition. All three leave the reader to work out that the field is one bit wide, and bit_extract() returns the value's type, not bool. bit_is_set() reads as the question it asks.

  • Template-only: the mask is a template argument, as with hal::bit_extract<mask>().
  • One bit only: a wider mask is a compile error (static_assert), since "set" would be ambiguous: any bit, or all of them? hal::bit_extract() covers wider fields.
  • No bit_is_clear(): not hal::bit_is_set<mask>(value) reads as well.
  • [[nodiscard]], and constexpr.
  • Same code: on a Cortex-M3 with Clang 23 at -Os, ((v >> 9) & 1) == 1, != 0 and (v & (1 << 9)) != 0 all compile to the same single ubfx, and the same lsls with a conditional move when they decide a branch. bit_is_set() is the first of these.

Testing

  • New tests in tests/bit.test.cpp: the lowest, a middle and the highest bit of a u32, the top bits of a u8 and a u64, and use at compile time.
  • New compile check tests/compile_checks/bit_rejects.cpp: a two-bit mask must fail with the static_assert message. Changing it to a one-bit mask makes the check fail, as it should.
  • conan build v6 with LLVM 23 on macOS: all 17 test suites pass, clang-tidy is clean, and the compile checks pass.

hal::bit_is_set<mask>(value) returns whether the one bit a single-bit
mask selects is 1, as a bool. It reads as the question it asks, where
comparing hal::bit_extract() with 0 or 1, or using it as a condition,
leaves the reader to work out that the field is one bit wide. A wider
mask fails to compile, since "set" would be ambiguous for it.

@kammce kammce left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅

@kammce
kammce merged commit f51880e into main Nov 14, 2026
9 of 10 checks passed
@kammce
kammce deleted the v6-bit-is-set branch November 14, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant