Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/23/02 12:50
Read: times


 
#31301 - RE: Inverting a byte - what is best?
Well, the '~' operator is the 'C' bitwise invert operator, so if that's what you want to do, that's the operator you should use.

Exclusive-ORing with 0xFF is a different operation which just happens to have the same effect in this particular case.
If you use this, you (probably) constrain the compiler to actually do an XOR with 0xFF - even if the target architecture might have a better way to invert each bit in a byte.

If you use the "proper" operator, you give the compiler to opportunity to chose the best implementation it can think of.
You also reduce the risk of being caught out if you change the size of your variable from 1 to, say, 2 bytes but forget to change your "mask" from 0xFF to 0xFFFF.

If you are concerned that your particular compiler may have a poor implementation of the '~' operator then, as Erik has already mentioned, you will have to check the generated code.
If you then decide to modify your coding style to compensate for your compiler's deficiencies, you will need to verify the results each time you update your tools!
eg,
#if defined( __C51__ )
// do it the C51 way
#else
// do it some other way
#endif


List of 8 messages in thread
TopicAuthorDate
Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      
RE: Inverting a byte - what is best?            01/01/70 00:00      

Back to Subject List