Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test bit by right shift same speed as left shift?

Is it the same speed to do

#define CHECK_BIT_BOOL(var,pos) ((var>>(pos)) & 1)

as

#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))

the goal is to NOT use BOOL macro like:

#define BOOL(x) (!(!(x)))

BOOL(CHECK_BIT(foo, 3));

it is nicer to do: CHECK_BIT_BOOL(foo,3);

general test bit question

like image 741
David Bonnin Avatar asked Feb 19 '26 03:02

David Bonnin


1 Answers

Current C compilers are very smart in translating typical code into the best machine language. Most of the time trying to outsmart the compiler will confuse it into generating dumb code.

Modern CPUs are able to do more than one operation at a time, whatever time this uses up will probably be shadowed by other operations, and make no difference. And CPUs are much faster than memory, most programs are more slowed down by memory access than computing.

Your time programming is much more valuable than any gain in runtime you can get by such microoptimizations, unless the code runs thousands of times a day on millions of machines. Concentrate on simple, clean, obviosly right and understandable code. When reading it next week you'll be grateful.

like image 186
vonbrand Avatar answered Feb 20 '26 17:02

vonbrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!