What are the bitwise operators and how can they be used in C++ software? Do you know what a bitwise operation is? What is bit shifting? How can we use the AND, OR, XOR, NOT bitwise operators? In this post we will answer these questions..

## A short explanation of what a ‘bit’ is in computer terminology

The construction, size, complexity and the general form of CPUs have changed enormously since 1950’s, on the other side the basic design and data processing technology has not changed much at all. Most CPUs today can be described as von Neumann stored-program machines. From the **1st Generation Computers** with **Vacuum Tubes** to today’s high-end **Fifth Generation Computers** with **Ultra Large-Scale Integration (ULSI) Technology** we store and process data in two forms, 1 and 0. This smallest data or processing information is called as **bit**, the name is a contraction of **binary digit**, it represents a logical state with one of two possible values. These values are most commonly represented as either “1”or”0″, but other representations such as true/false, yes/no, +/−, or on/off are common.

## What are bitwise operations?

The bit is the most basic unit of information in computing and digital communications. In reality all operators are mainly based on **Bit Operations** which are also called **Bitwise Operations**. In computer programming, a **Bitwise Operation** operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits, 1s and 0s. The Bitwise Operation is basic to the higher level arithmetic operations and it is a fast and simple action, because it is directly supported by the processors. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

Because of all these basics of the micro architecture of computers, it is very important to know Bitwise Operators. The C Programming language is one of the oldest programming languages. A lot of operands, operators in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages. We have explained well about operators in general in this Learn How To Use Operators In C++ post before. Now let’s see Bitwise Operators.

## What are the C and C++ software bitwise operators?

#### AND Operator

The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

1 2 3 4 5 |
bool a=true, b=true, c; c = a & b; |

#### OR Operator

The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

1 2 3 4 5 |
bool a=true, b=false, c; c = a | b; |

#### XOR Operator

The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

1 2 3 4 5 |
bool a=true, b=false, c; c = a ^ b; |

#### NOT Operator

The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it

1 2 3 4 5 |
bool a=false, b=false; b = ~a ; |

#### Left Shift Operator

The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. The >> (right shift) in C or

1 2 3 4 5 |
int A = 65, B; B = A << 1; |

#### Right Shift Operator

C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

1 2 3 4 5 |
int A = 65, B; B = A >> 1; |

Design. Code. Compile. Deploy.

Start Free Trial

Free C++Builder Community Edition