Numbers in cpp

In C++, numbers are represented using various data types. Integer data types, such as int, short, long, and long long, handle whole numbers without fractional parts. Floating-point data types, like float, double, and long double, deal with numbers that include a fractional component. Basic arithmetic operations like addition, subtraction, multiplication, division, and modulus are used to perform calculations on numbers in C++. Typecasting allows converting data from one type to another. Implicit typecasting is automatic, while explicit typecasting involves casting operators like static_cast, dynamic_cast, const_cast, and reinterpret_cast. Understanding these concepts is essential for effective number manipulation in C++.

Integer Numbers in C++

A. Integer data types in C++ (e.g., int, short, long, etc.)

int: Represents signed integers with a typical range of -2147483648 to 2147483647.

short: Represents signed short integers with a range of -32768 to 32767.

long: Represents signed long integers with a range of -2147483648 to 2147483647.

long long: Represents signed long long integers with a range of -9223372036854775808 to 9223372036854775807.

B. Range and size of integer data types

The sizeof operator can be used to determine the size (in bytes) of each data type.

The limits header provides constants like INT_MIN, INT_MAX, LONG_MIN, LONG_MAX, etc., to access the minimum and maximum values of integer data types.

C. Declaring and initializing integer variables

#include <iostream>
int main() {
    int myInt;
    myInt = 42;
    short myShort = -32768;
    long myLong(1234567890);
    long long myLongLong = 922372036854775807;
    std::cout << "myInt: " << myInt << std::endl;
    std::cout << "myShort: " << myShort << std::endl;
    std::cout << "myLong: " << myLong << std::endl;
    std::cout << "myLongLong: " << myLongLong << std::endl;
    return 0;
}

D. Arithmetic operations with integers

#include <iostream>
int main() {
    int num1= 10;
    int num2 = 5;
    int sum = num1 + num2;
    int difference = num1 - num2;
    int product = num1 * num2;
    int quotient = num1 / num2;
    int remainder = num1 % num2.
    std::cout << "Sum: " << sum << std::endl;
    std::cout << "Difference: " << difference << std::endl;
    std::cout << "Product: " << product << std::endl;
    std::cout << "Quotient: " << quotient << std::endl;
    std::cout << "Remainder: " << remainder << std::endl;
    return 0;
}

E. Increment and decrement operators

#include <iostream>
int main() {
    int num = 5;
    std::cout << "Original value: " << num << std::endl;
    num++; // Postfix increment
    std::cout << "After postfix increment: " <<num << std::endl;
    ++num; // Prefix increment
    std::cout << "After prefix increment: " << num << std::endl;
    num--; // Postfix decrement
    std::cout << "After postfix decrement: " <<num << std::endl;
    --num; // Prefix decrement
    std::cout << "After prefix decrement: " << num << std::endl;
    return 0;
}

F. Bitwise operations on integers

#include <iostream>
int main() { 
    int num1 = 5; // 00000101 in binary
    int num2 = 3; // 00000011 in binary
    int bitwiseAnd = num1 & num2; // Bitwise AND
    int bitwiseOr = num1 | num2; // Bitwise OR
    int bitwiseXor = num1 ^ num2; // Bitwise XOR
    int bitwiseComplement = ~num1; // Bitwise complement
    int leftShift = num1 << 2; // Left shift by 2 positions
    int rightShift = num1 >> 2; //Right shift by 2 positions
    std::cout << "Bitwise AND: " << bitwiseAnd <<<< std::endl;
    std::cout << "Bitwise OR: " << bitwiseOr << std::endl;
    std::cout << "Bitwise XOR: " << bitwiseXor <<< std::endl;
    std::cout << "Bitwise complement:  " << bitwiseComplement << std::endl;
    std::cout << "Left shift: " << leftShift <<< std::endl;
    std::cout << "Right shift: " << rightShift <<< std::endl;
    return 0;
}

Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs