John Regehr on Nostr: during the course of trying to explain my near-obsession with invariants to my older ...
during the course of trying to explain my near-obsession with invariants to my older kid (a 2nd year CS student), I showed him this code and asked if he could come up with the key loop invariant that results in a strong enough postcondition that we get a proof of correctness, and he got it!
int popcount_kernighan(uint64_t x) {
int c = 0;
while (x) {
x &= x - 1;
c++;
}
return c;
}
Published at
2024-11-28 04:33:51Event JSON
{
"id": "9fe8208b0ab9eb5f1a8b062f42ae8e57f7a5dadb30398c875fc00c5bd63e81be",
"pubkey": "a1a4eb540235341a2db0d8a08cfb74edc4bb06b316c7bb39580c55559ce71ba1",
"created_at": 1732768431,
"kind": 1,
"tags": [
[
"proxy",
"https://mastodon.social/users/regehr/statuses/113558711898427458",
"activitypub"
]
],
"content": "during the course of trying to explain my near-obsession with invariants to my older kid (a 2nd year CS student), I showed him this code and asked if he could come up with the key loop invariant that results in a strong enough postcondition that we get a proof of correctness, and he got it!\n\nint popcount_kernighan(uint64_t x) {\n int c = 0;\n while (x) {\n x \u0026= x - 1;\n c++;\n }\n return c;\n}",
"sig": "eb6ec8e39091346fba15579c3bf560bde3acea659561fdf0baab8610982bd85ce22fdfa526fdb28069df4052ba77ed520644067e7de212105f8a8ffb977ff52e"
}