Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Some more Operators

User image

Published by

sanya sanya

Published at: 27th Jul, 2023
2.09 mins read

Arithmetic - [ ++ , -- ]

Arithmetic operators in C++ are used for performing mathematical calculations. The ‘++’ operator increments the value of a variable by 1, and the ‘--’ operator decrements the value by 1. These operators can be used in both prefix and postfix forms.

- Prefix form (++variable, --variable):

int x = 5;

++x; // Increment x by 1

--x; // Decrement x by 1

- Postfix form (variable++, variable--):

int x = 5;

x++; // Increment x by 1

x--; // Decrement x by 1

Arithmetic operators are commonly used for loop counters, incrementing or decrementing variables, and performing iterative calculations.

Bitwise Operators - [ &, | , ~, ^, <<, >>]

Bitwise operators in C++ are used for manipulating individual bits of integer operands.

- Bitwise AND (‘&’):

int a = 5; // 0101 in binary

int b = 3; // 0011 in binary

int result = a & b; // Bitwise AND of a and b

- Bitwise OR (‘|’):

int a = 5; // 0101 in binary

int b = 3; // 0011 in binary

int result = a | b; // Bitwise OR of a and b

- Bitwise NOT (‘~’):

int a = 5; // 0101 in binary

int result = ~a; // Bitwise NOT of a

- Bitwise XOR (‘^’):

int a = 5; // 0101 in binary

int b = 3; // 0011 in binary

int result = a ^ b; // Bitwise XOR of a and b

- Left Shift (‘<<‘):

int a = 5; // 0101 in binary

int result = a << 2; // Left shift a by 2 bits

- Right Shift (‘>>‘):

int a = 5; // 0101 in binary

int result = a >> 2; // Right shift a by 2 bits

Bitwise operators are useful in low-level programming, dealing with flags, manipulating individual bits, and performing certain optimizations.

Compound assignment operators - [ +=, *=, /=, %=, &=, |=, ^=, <<=, >>= ]

Compound assignment operators combine an arithmetic or bitwise operation with assignment. They modify the value of a variable and assign the result back to the same variable.

int x = 5;

x += 2; // Equivalent to x = x + 2;

x *= 3; // Equivalent to x = x * 3;

x >>= 1; // Equivalent to x = x >> 1;

Compound assignment operators provide a concise way to update variables by performing an operation and assigning the result in a single step.

Library

WEB DEVELOPMENT

FAANG QUESTIONS