C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Use Bitwise Operators in C++ Software

This Is How To Use Bitwise Operators in C++ Software

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.

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.

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.

NOT Operator

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

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

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

What Is The Erase WString Method In A Modern C++ App

C++C++17Introduction to C++Learn C++

This Is How Trivial Copy Constructors Work In A C++ App

C++Learn C++

Solve 5 Diagonal Matrices In C++ Software For Engineers

C++C++11C++14C++17Introduction to C++Learn C++

This Is How String Capacity And Lengths Work In A C++ App