Skip to content

Commit f2fd41e

Browse files
Merge pull request #10 from dreamer-coding/main
2 parents f52a0ee + 0f39537 commit f2fd41e

File tree

4 files changed

+578
-20
lines changed

4 files changed

+578
-20
lines changed

code/logic/bitwise.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,45 @@ int fossil_sys_bitwise_lookup(const char *name, const fossil_sys_bitwise_table_t
7373
}
7474
return -1; // Not found
7575
}
76+
77+
uint64_t fossil_sys_bitwise_all(const fossil_sys_bitwise_table_t *table) {
78+
if (!table || !table->entries) return 0;
79+
80+
uint64_t mask = 0;
81+
for (size_t i = 0; i < table->count; i++) {
82+
mask |= table->entries[i].bit;
83+
}
84+
return mask;
85+
}
86+
87+
int fossil_sys_bitwise_validate(uint64_t bits, const fossil_sys_bitwise_table_t *table) {
88+
if (!table) return -1; // invalid table pointer
89+
90+
uint64_t all = fossil_sys_bitwise_all(table);
91+
return (bits & ~all) ? -1 : 0; // -1 if unknown bits are set
92+
}
93+
94+
const char *fossil_sys_bitwise_name(uint64_t bit, const fossil_sys_bitwise_table_t *table) {
95+
if (!table || !table->entries) return NULL;
96+
97+
for (size_t i = 0; i < table->count; i++) {
98+
if (table->entries[i].bit == bit) {
99+
return table->entries[i].name;
100+
}
101+
}
102+
return NULL;
103+
}
104+
105+
size_t fossil_sys_bitwise_count(uint64_t bits) {
106+
#if defined(__GNUC__) || defined(__clang__)
107+
return (size_t)__builtin_popcountll(bits);
108+
#else
109+
// Portable fallback (Brian Kernighan’s bit-counting algorithm)
110+
size_t count = 0;
111+
while (bits) {
112+
bits &= (bits - 1); // clear the lowest set bit
113+
count++;
114+
}
115+
return count;
116+
#endif
117+
}

0 commit comments

Comments
 (0)