Jump to content

Bitwise operators


Recommended Posts

Posted

Hey, this is my first post!

i was reading through some mangos source code and i'm really interested in how this emulator works. im quite new to c++ but i cant help but notice that people dont use bitwise operators. like so:

if (a&b) 
   {
    action
   }

instead of

if (a&&b) 
   {
    action
   }

now bitwise operators are faster. if 'a' is false it automaticly stops processing and return 0, it doesnt check the other variable. so i was interested and did some tests. this is what came out:

**********************************
bitwise [00]: 2235 nanoseconds result: false  
bitwise [10]: 1676 nanoseconds result: false  
bitwise [01]: 2235 nanoseconds result: false  
bitwise [11]: 1676 nanoseconds result: true  
**********************************
non bitwise [00]: 2793 nanoseconds result: false  
non bitwise [10]: 1955 nanoseconds result: false  
non bitwise [01]: 1955 nanoseconds result: false  
non bitwise [11]: 1677 nanoseconds result: true  
**********************************

as you can see, using bitwise operators is at least 10% faster then not using bitwise operators. for the testing i used:

        if (a && b) {
           result = true;
       } else {
           result = false;
       }

the using of bitwise operators seems faster when using while, will this help you guys in any way?

Posted

Totally wrong.

1) in if(a && b) only _a_ calculated if it have false result and _b_ not executed and save time for program work

in if(a & b) _both a and b calculated that can be lot more slow

2) in C/C++ any non-0 values is true values and then f(4 && 2) is laways true

BUT if(4 & 2) is always false.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use