Binary Logic Bit Operations In C and C++
Programming Practice
Common Synonyms for True and False | ||||
---|---|---|---|---|
0 | false | off | unset | zero |
1 | true | on | set | non-zero |
In C/C++, anything other than zero is considered a true value in statements.
Many Windows API functions return zero in the common case and non-zero in the failure case.
Note that they do not use macros such as TRUE, FALSE, or BOOLEAN,
as these may cause global namespace conflicts, type initialization errors,
and other portability problems. In statements, I suggest checking
for the expected value explicitly.
// Check for the expected return value explicitly, to avoid ambiguity. if(GetErrorStatus() != 0) { printf("Error"); } // This way is logically equivalent, but less clear. if(GetErrorStatus()) { printf("Error"); }
C Bitwise Operators
& | binary bitwise AND |
---|---|
^ | binary bitwise exclusive OR (XOR) |
| | binary bitwise inclusive OR |
~ | unary bitwise complement (NOT) |
An operand is the variable or value on which the operator acts.
Bitwise operators perform the given operation on each bit in the operand.
Binary means the operator operates on two operands and unary
means the operator operates on a single operand.
Toggling a bit and leaving all other bits unchanged
x = x ^ mask;
(or shorthand x ^= mask;)
Bits that are set to 1 in the mask will be toggled in x.
Bits that are set to 0 in the mask will be unchanged in x.
Toggling means that if the bit is 1, it is set to 0, and if the bit is 0, it is set to 1.
XOR truth table 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0
Setting a bit to zero and leaving all other bits unchanged
x = x & (~mask);
(or x &= (~mask);)
Bits that are set to 1 in the mask will be set to zero in x.
Bits that are set to 0 in the mask will be unchanged in x.
AND truth table 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
Setting a bit to one and leaving all other bits unchanged
x = x | mask;
(or x |= mask;)
Bits that are set to 1 in the mask will be set to one in x.
Bits that are set to 0 in the mask will be unchanged in x.
OR truth table 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1
Common Flag Values
I prefer to use the hexadecimal representation in my code.
Binary (base2) |
Hexadecimal (base16) |
Decimal (base10) |
---|---|---|
0000 0000 | 0x00 | 0 |
0000 0001 | 0x01 | 1 |
0000 0010 | 0x02 | 2 |
0000 0100 | 0x04 | 4 |
0000 1000 | 0x08 | 8 |
0001 0000 | 0x10 | 16 |
0010 0000 | 0x20 | 32 |
0100 0000 | 0x40 | 64 |
1000 0000 | 0x80 | 128 |
Javascript Base Conversion Calculator
Example Macros
Imagine there are two flags in the program that are independent of each other. We might implement macros to manipulate them as shown in the code sample below. It would probably be wise to put the macros in a header file.
// the flag definitions
#define CAR_LOCKED 0x01// 0000 0001
#define CAR_PARKED 0x02// 0000 0010
#define CAR_RESET 0x00// 0000 0000
// macros to manipulate the flags
#define RESET_CAR(x) (x = CAR_RESET) #define SET_LOCKED(x) (x |= CAR_LOCKED) #define SET_PARKED(x) (x |= CAR_PARKED) #define UNSET_LOCKED(x) (x &= (~CAR_LOCKED)) #define UNSET_PARKED(x) (x &= (~CAR_PARKED)) #define TOGGLE_LOCKED(x) (x ^= CAR_LOCKED) #define TOGGLE_PARKED(x) (x ^= CAR_PARKED)// these evaluate to non-zero if the flag is set
#define IS_LOCKED(x) (x & CAR_LOCKED) #define IS_PARKED(x) (x & CAR_PARKED)// a short program that demonstrates how to use the macros
int main(void) { unsigned char fMercedes, fCivic; RESET_CAR(fMercedes); RESET_CAR(fCivic); SET_LOCKED(fMercedes); if( IS_LOCKED(fMercedes) != 0 ) { UNSET_PARKED(fCivic); } TOGGLE_LOCKED(fMercedes); return 0; }
These C programming notes were written January 10, 2003.
Disclaimer: This content is provided as-is. The information may be incorrect.