0% found this document useful (0 votes)
8 views1 page

C++ Basic Arithmetic and Logic Operations

The document is a C++ code that demonstrates the use of various operators including arithmetic, relational, logical, and bitwise operators. It includes examples of incrementing and decrementing variables, as well as conditional statements to evaluate expressions. Additionally, it showcases short-circuit evaluation and bitwise operations on integers.

Uploaded by

aajainb23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

C++ Basic Arithmetic and Logic Operations

The document is a C++ code that demonstrates the use of various operators including arithmetic, relational, logical, and bitwise operators. It includes examples of incrementing and decrementing variables, as well as conditional statements to evaluate expressions. Additionally, it showcases short-circuit evaluation and bitwise operations on integers.

Uploaded by

aajainb23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream> #include <iostream> result = (char1 < char2);

using namespace std; cout << "Initial count: " << count << endl; using namespace std; cout << char1 << " < " << char2 << ": " << result << endl;

int main() count++; //Post-increment int main() { //Demonstrating how relational operators are used in conditional statements
{ cout << "Post-increment: " << count << endl; int num1 = 10;
// Declare variables int num2 = 5; if (num1 > num2) {
int num1 = 10; ++count; //Pre-increment bool result; // To store the boolean result of relational operations cout << num1 << " is greater than " << num2 << endl;
int num2 = 5; cout << "Pre-increment: " << count << endl; } else {
int result; // Equal to (==) cout << num1 << " is not greater than " << num2 << endl;
count--; //Post-decrement result = (num1 == num2); }
// Addition cout << "Post-decrement: " << count << endl; cout << num1 << " == " << num2 << ": " << result << endl;
result = num1 + num2; return 0;
cout << "Addition: " << num1 << " + " << num2 << " = " << result << endl; --count; //Pre-decrement // Not equal to (!=)
cout << "Pre-decrement: " << count << endl; result = (num1 != num2);
// Subtraction cout << num1 << " != " << num2 << ": " << result << endl;
result = num1 - num2; //Compound assignment operators
cout << "Subtraction: " << num1 << " - " << num2 << " = " << result << endl; int value = 5; // Greater than (>)
result = (num1 > num2);
// Multiplication value += 3; // Equivalent to value = value + 3; cout << num1 << " > " << num2 << ": " << result << endl;
result = num1 * num2; cout << "value += 3: " << value << endl;
cout << "Multiplication: " << num1 << " * " << num2 << " = " << result << endl; // Less than (<)
value -= 2; // Equivalent to value = value - 2; result = (num1 < num2);
// Division cout << "value -= 2: " << value << endl; cout << num1 << " < " << num2 << ": " << result << endl;
result = num1 / num2;
cout << "Division: " << num1 << " / " << num2 << " = " << result << endl; value *= 4; // Equivalent to value = value * 4; // Greater than or equal to (>=)
cout << "value *= 4: " << value << endl; result = (num1 >= num2);
// Modulus (remainder) cout << num1 << " >= " << num2 << ": " << result << endl;
result = num1 % num2; value /= 2; // Equivalent to value = value / 2;
cout << "Modulus: " << num1 << " % " << num2 << " = " << result << endl; cout << "value /= 2: " << value << endl; // Less than or equal to (<=)
result = (num1 <= num2);
//Demonstrating integer division and floating point division value %= 3; // Equivalent to value = value % 3; cout << num1 << " <= " << num2 << ": " << result << endl;
double num3 = 10.0; cout << "value %= 3: " << value << endl;
double num4 = 3.0; //Demonstrating relational operators with different data types.
int intResult = num3 / num4; return 0;
double doubleResult = num3 / num4; double double1 = 7.5;
double double2 = 7.5;
cout << "Integer division (10/3): " << intResult << endl; //will truncate the decimal result = (double1 == double2);
cout << "Floating point division (10.0/3.0): " << doubleResult << endl; cout << double1 << " == " << double2 << ": " << result << endl;

//Increment and decrement operators char char1 = 'a';


int count = 0; char char2 = 'b';

#include <iostream> cout << "Short circuit OR, count: " << count << endl; #include <iostream> } else {
using namespace std; } #include <bitset> // For easy visualization of binary cout << number << " is odd." << endl;
using namespace std; }
int main() { count = 0;
bool a = true; int main() { number = 8;
bool b = false; if (b && ++count) //count will not increment because b is false. unsigned int num1 = 60; // 60 = 0011 1100 in binary if((number & 1) == 0){
{ unsigned int num2 = 13; // 13 = 0000 1101 in binary cout << number << " is even." << endl;
// Logical AND (&&) cout << "Short circuit AND, count: " << count << endl; } else {
cout << "a && b: " << (a && b) << endl; // false } cout << "num1 (60): " << bitset(num1) << endl; cout << number << " is odd." << endl;
cout << "a && true: " << (a && true) << endl; //true cout << "num2 (13): " << bitset(num2) << endl; }
cout << "Final count: " << count << endl;
// Logical OR (||) // Bitwise AND (&) return 0;
cout << "a || b: " << (a || b) << endl; // true return 0; unsigned int result_and = num1 & num2; // 12 = 0000 1100 }
cout << "false || b: " << (false || b) << endl; //false } cout << "num1 & num2: " << bitset(result_and) << " (" << result_and << ")" << endl;

// Logical NOT (!) // Bitwise OR (|)


cout << "!a: " << !a << endl; // false unsigned int result_or = num1 | num2; // 61 = 0011 1101
cout << "!b: " << !b << endl; // true cout << "num1 | num2: " << bitset(result_or) << " (" << result_or << ")" << endl;

// Example with conditional statements // Bitwise XOR (^)


int x = 10; unsigned int result_xor = num1 ^ num2; // 49 = 0011 0001
int y = 5; cout << "num1 ^ num2: " << bitset(result_xor) << " (" << result_xor << ")" << endl;

if (x > 0 && y > 0) { // Bitwise NOT (~)


cout << "Both x and y are positive." << endl; unsigned int result_not_num1 = ~num1; // -61 = 1100 0011 (two's complement)
} cout << "~num1: " << bitset(result_not_num1) << " (" << result_not_num1 << ")" << endl;
//Use 32 bits to show the full result
if (x > 10 || y > 0) {
cout << "Either x is greater than 10 or y is greater than 0." << endl; // Left shift (<<)
} unsigned int result_left_shift = num1 << 2; // 240 = 1111 0000
cout << "num1 << 2: " << bitset(result_left_shift) << " (" << result_left_shift << ")" << endl;
if (!(x == y)) {
cout << "x is not equal to y." << endl; // Right shift (>>)
} unsigned int result_right_shift = num1 >> 2; // 15 = 0000 1111
cout << "num1 >> 2: " << bitset(result_right_shift) << " (" << result_right_shift << ")" <<
//Demonstration of short circuit evaluation. endl;

int count = 0; //Example of using bitwise operator to check if a number is even or odd
int number = 7;
if(a || ++count) //count will not increment because a is true. if((number & 1) == 0){
{ cout << number << " is even." << endl;

You might also like