Here are two ways to set an individual bit in C on x86-64:
inline void SetBitC(long *array, int bit) {
//Pure C version
*array |= 1<
Why does GCC optimize so poorly for such a common operation?
Prelude: Since the late 1980s, focus on compiler optimization has moved away from microbenchmarks which focus on individual operations and toward macrobenchmarks which focus on applications whose speed people care about. These days most compiler writers are focused on macrobenchmarks, and developing good benchmark suites is something that is taken seriously.
Answer: Nobody on the gcc is using a benchmark where the difference between or
and bts
matters to the execution time of a real program. If you can produce such a program, you might be able to get the attention of people in gcc-land.
Am I doing something wrong with the C version?
No, this is perfectly good standard C. Very readable and idiomatic, in fact.