Wednesday, May 20, 2009

Bitwise Operators

C++ Bitwise Operators


The bitwise operators are:

Bitwise AND (&)

Bitwise exclusive OR (^)

Bitwise inclusive OR (|)

These operators return bitwise combinations of their operands.

and-expression

equality-expression

and-expression & equality-expression

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.

Both operands to the bitwise AND operator must be of integral types. The usual arithmetic conversions covered in Arithmetic Conversions, are applied to the operands.

Operator Keyword for &

The bitand operator is the text equivalent of &. There are two ways to access the bitand operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

Example

#include


using namespace std;

int main()
{

unsigned short a = 0xFFFF; // pattern 1111 ...

unsigned short b = 0xAAAA; // pattern 1010 ...


cout <<>
}


exclusive-or-expression
and-expression
exclusive-or-expression ^ and-expression

The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Both operands to the bitwise exclusive OR operator must be of integral types. The usual arithmetic conversions covered in Arithmetic Conversions are applied to the operands.

Operator Keyword for ^


The xor operator is the text equivalent of ^. There are two ways to access the xor operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

Example:

#include

using namespace std;

int main()
{

unsigned short a = 0x5555; // pattern 0101 ...

unsigned short b = 0xFFFF; // pattern 1111 ...


cout <<>

}

Bitwise Inclusive OR Operator

inclusive-or-expression
exclusive-or-expression
inclusive-or-expression | exclusive-or-expression

The bitwise inclusive OR operator (|) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Both operands to the bitwise inclusive OR operator must be of integral types. The usual arithmetic conversions covered in Arithmetic Conversions are applied to the operands.

Operator Keyword for |

The bitor operator is the text equivalent of |. There are two ways to access the bitor operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

Example


#include

using namespace std;


int main()
{

unsigned short a = 0x5555; // pattern 0101 ...

unsigned short b = 0xAAAA; // pattern 1010 ...


cout <<>

}

No comments:

Post a Comment