Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I need help with the code below. Especially with the sentence (~(a) & 1)
What does it means?

C++
#define Check(a) (~(a) & 1)

#define MY_Check(ts,string)                 
    if (Check(ts))                           
    {                                           
        report_event....
        return sts;                     
    }
Posted
Updated 1-Jun-15 22:40pm
v2

1. the operator ~ performs a bitwise negation of the binary representation of a given value. Likewise, & also operates on the binary representation of a bvalue. Unless you define that binary representaion yourself (e. g. in a bit field), there is no gurantee this will work as expected on ever compiler, because a compiler is not guranteed to use the same binary representation for each type of variable.

2. Don't use #define, at least not for a purpose that can easily be written as a function!

3. The second macro has several issues: it's lacking the line continuation character '\', so the macro definition will be empty, and the subsequential lines will be interpreted as actual code, not part of a macro; also, the returned value sts doesn't appear to be defined (although the .... appears to imply you've omitted code that might define it)

4. This has got nothing to do with C++, it's low-level C, in fact so low you might as well use assembler. If you intend to write C++ programs, I suggest you forget about both bit operators and #define. If you have a good reason to stick to C instead, please tag the question as such, so we know not to offer advice on good C++ programming. Issue 1 applies either way!
 
Share this answer
 
There are two operators:

The first one:

C++
~ (bitweise Negation)


~(10) means (01)



And the second:
C++
The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. 


(01) & (11) means (01)
 
Share this answer
 
v3
Comments
Frankie-C 2-Jun-15 5:53am    
to avoid confusion to the beginner would be better to use the correct terms:
The AND operator performs a logical AND operation between each couple of bits occupying the same position in the two operands and producing the result in the same bit position in the result. I.e. 010101 AND 101100 = 000100.
The AND operation is *not* a comparison.
It is sometimes called *masking* operation because, due to the AND operation nature, you can craft a *mask* where all bits set to 1 are reported on ther esult, while bits set to 0 are masked to 0. I.e. 01010101 AND 11110000 = 01010000 => the first upper 4 bits are reported as they are because the mask have that bits set, while the lower 4 bits are set to 0 because the mask have that bits to 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900