0% found this document useful (0 votes)
72 views5 pages

Binary Logic Bit Operations in C and C++: Programming Practice

The document discusses binary logic and bit operations in C and C++. It defines common synonyms for true and false values, explains how non-zero values are considered true in statements, and describes several bitwise operators - AND, OR, XOR, and NOT - and their truth tables. It also provides examples of using bitwise operators to toggle, set, or clear individual bits by masking them. Finally, it shows some common flag values in binary, hexadecimal, and decimal and demonstrates how to define macros to manipulate flags.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views5 pages

Binary Logic Bit Operations in C and C++: Programming Practice

The document discusses binary logic and bit operations in C and C++. It defines common synonyms for true and false values, explains how non-zero values are considered true in statements, and describes several bitwise operators - AND, OR, XOR, and NOT - and their truth tables. It also provides examples of using bitwise operators to toggle, set, or clear individual bits by masking them. Finally, it shows some common flag values in binary, hexadecimal, and decimal and demonstrates how to define macros to manipulate flags.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 unchanged in x.


Bits that are set to 0 in the mask will be set to zero 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

Operator/
Languages/ Returned Value Example Example Result
Operation
Two-place operator,
& 0000 1100
return a word with each x = 12;
& 0000 1010
bit set only if the y = 10;
---------
C, C++, Java
corresponding bit is set z = x & y; // z is 8 0000 1000 = 8 (decimal)
Bitwise And
in both operands.
Two-place operator,
|
return a word with each 0000 1100
x = 12;
C, C++, Java bit set if either or both y = 10;
| 0000 1010
---------
Bitwise Or of the the corresponding z = x | y; // z is 14
0000 1110 = 14 (decimal)
bits are set in the
operands.
Two-place operator,
return a word with each
^
bit set only if the
0000 1100
C, C++, Java corresponding bit in one x = 12; ^ 0000 1010
Bitwise operand or the other but y = 10; ---------
z = x ^ y; // z is 6
Exclusive Or not both is set. This 0000 0110 = 6 (decimal)
operator can be thought
of as the bitwise not
equal to operator.
~ ~ 0000 1100
C, C++, Java Unary (one operand),
---------
Bitwise Not return a word with ones x = 12;
1111 0011 = -13
changed to zeros and z = ~ x // z is -13
(One's (8-bit two's
Complement) zeros to ones. complement decimal)

Two-place operator,
return a word with each 0000 1100
bit shifted toward the //
<< high end of the word by // shifted 3 places
C, C++, Java the specified number of x = 12;
z = x << 3; // z is 96
//
Left Shift vv
bits. ---------
x << y
0110 0000 = 96 (decimal
is equivalent to
x * 2y

Two-place operator, 0000 1100


\\
return a word with each
\\ shifted 3 places
bit shifted toward the x = 12; /0000 1100
\+-> (bits lost)
low end of the word by z = x >> 3; // z is 1 v
the specified number of ---------
>> 0000 0001 = 1 (decimal)
bits. The places at the
C, C++, Java
high end of the word
Right Shift,
which were vacated by 1111 0011
Sign-Filled \ \\
the shift are filled with
\ ...\\ shifted 3 places
the sign (high) bit. x = -13; //1111 0011
\ \+-> (bits lost)
x >> y z = x >> 3; // z is -2
v v
---------
is equivalent to 1111 1110 = -2 (decimal)
x / 2y

Common Flag Values


Binary Hexadecimal Decimal
(base2) (base16) (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
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;
}

You might also like