Instagram
youtube
Facebook
Twitter

Basic Operations in C++

Basic Operations in C++

In C++, there are several basic operations that you can perform:

Arithmetic Operations: C++ supports introductory arithmetic operations, including addition (+), deduction (-), multiplication(*), division (/), and modulus (%).

Example:

int x = 15;

int y = 5;

int sum = x + y;

int difference = x - y;

int product = x * y;

int quotient = x / y;

int remainder = x % b;

 

Assignment: You can assign values to variables using the assignment operator (=).

Example

int x = 15;

int y = x;

 

Increment and Decrement: C++ provides proliferation () and decrement (--) drivers to increase or decrease the value of a variable by 1. They can be used in both prefix forms.

Example

int a = 5;

a++; // Postfix increment: equivalent to a = 

a+1

++a;    //Prefix increment: equivalent to a = a

+1

int b = 10;

b--;    //Postfix decrement: equivalent to b = b - 1

--b;     //Prefix decrement: equivalent to b = b - 1

 

Comparison Operations: C++ provides several operators to compare values, including equal to (==), not equal to (!=), greater than(>), lower than(<), greater than or equal to (=). Based on the comparison result, these operators return a Boolean value (true or false).

Example

int a = 15;

int b = 5;

bool isEqual = (a == b);

bool isGreater = (a > b);

 

Logical Operations: C++ supports logical operations similar to logical AND(&&), logical OR (||), and logical NOT(!). These operators are used to combine multiple conditions or invert the result of a condition.

  • Logical OR(‘||’): Return ‘true’ if at least one of the operands is ‘true’ ,otherwise returns ‘false’.
  • Logical NOT(‘!’): Return the opposite of the operand’s value; ‘true’ becomes ‘false’ and vice versa.
  • Logical AND(‘&&’): Return ‘true’ if both operands are ‘true’, otherwise returns ‘false’.
  • Short-Circuit Evaluation: In C++,logical AND(‘&&’) and logical OR(‘||’) operators perform short-circuit evaluation. If the result can be determined by evaluating only one operand, the other operand is not evaluated.

Sample Code

bool condition1 = true;

bool condition2 = false;

bool result1 = (condition1 && condition2); //logical AND

bool result2 = (condition1 || condition2);  //Logical OR

bool result3 =!condition1;  //Logical NOT

 

Bitwise Operations: Bitwise operations are used to manipulate individual bits of data at the binary level. Then are the main bitwise operations in C++, along with code illustrations.

  • Bitwise AND(‘&’): Performs a bitwise AND operation between each pair of corresponding bits of two integers.
#include <iostream>
using namespace std;
 
int main() {
      int a = 5;
      int b = 3;
      int result = a & b;

      cout << “Result of Bitwise AND: ” << result << endl;
     
      return 0;
}

 

  • Bitwise OR(‘I’): Performs a bitwise OR operation between each pair of corresponding bits of two integers.
#include<iostream>
using namespace std;

int main() {
      int a = 5;
      int b = 3;
      int result = a | b;
 
      cout << “Result of Bitwise OR: ” << result << endl;

      return 0;
}
  • Bitwise XOR(‘^’): Performs a bitwise XOR (exclusive OR) operation between each pair of corresponding bits of two integers.
#include<iostream>
using namespace std;

int main() {
      int a = 5;
      int b = 3;
      int result = a ^ b;
     
      cout << “Result of Bitwise XOR: ” << result <<endl;
   
      return 0;
}

 

 

  • Bitwise NOT (‘~’): Flips each bit of the integer, changing 0s to 1s and vice versa.
#include<iostream>
using namespace std;

int main() {
      int a = 5;
      int result = ~a;
      
       cout << “Result of Bitwise NOT: ”<< result << endl;

          return 0;
      }
  • Left Shift(‘<<’)and Right Shift(‘>>’): Shifts the bits of an integer left or right by a specified number of positions.
#include<iostream>
using namespace std;

int main() {
      int num = 10;
      int leftShifted = num << 2;
      int rightShifted = num >> 1;

      cout << “Left Shifted Results: ” << leftShifted << endl;
      cout << “Right Shifted Results: ” << rightShifted << endl;

      return 0;
 }    

 

These examples demonstrate the basic bitwise operations in C++. Bitwise operations are often used in low-level programming and optimization tasks.
 

These are just some of the introductory operations in C++. C++ also provides numerous other operators and features for performing advanced operations and calculations.