Understanding Binary and Assignment Operators

Aug 11, 2024

Lecture Notes: Binary Operators and Assignment Operators

Lecturer: Abdurrahman

Previous Lesson Recap

  • Topic: Bitwise Operators
  • Operators Covered:
    • Bitwise AND
    • Bitwise OR
    • Exclusive OR
    • Shift Left
    • Shift Right
    • Complement

Current Lesson: Binary Operators

Assignment Operators

  • Introduction: Binary operators in the context of assignment operators. There are six assignment operators.
1. Assignment (=)
  • Symbol: =
  • Function: Assigns a value to a variable
2. Add and Assign (+=)
  • Symbol: +=
  • Function: Adds value to the variable and then assigns the result to the variable
3. Subtract and Assign (-=)
  • Symbol: -=
  • Function: Subtracts value from the variable and then assigns the result to the variable
4. Multiply and Assign (*=)
  • Symbol: *=
  • Function: Multiplies the variable by a value and then assigns the result to the variable
5. Divide and Assign (/=)
  • Symbol: /=
  • Function: Divides the variable by a value and then assigns the result to the variable
6. Modulus and Assign (%=)
  • Symbol: %=
  • Function: Takes the modulus of the variable with a value and then assigns the result to the variable

Examples and Implementation in C++

  • Initialization:
    • int a = 10;
    • int b = 20;

Examples

  • Add and Assign:

    • a += b; is equivalent to a = a + b;
    • Example: If a = 10 and b = 20, then a becomes 30
  • Subtract and Assign:

    • a -= b; is equivalent to a = a - b;
    • Example: If a = 40 and b = 20, then a becomes 20
  • Multiply and Assign:

    • a *= b; is equivalent to a = a * b;
    • Example: If a = 20 and b = 20, then a becomes 400
  • Divide and Assign:

    • a /= b; is equivalent to a = a / b;
    • Example: If a = 400 and b = 20, then a becomes 20
  • Modulus and Assign:

    • a %= b; is equivalent to a = a % b;
    • Example: If a = 20 and b = 20, then a becomes 0

Summary of Output from Program

  • Assignment Operator (=): Final value of a is 20
  • Add and Assign (+=): Final value of a is 40
  • Subtract and Assign (-=): Final value of a is 20
  • Multiply and Assign (*=): Final value of a is 400
  • Divide and Assign (/=): Final value of a is 20
  • Modulus and Assign (%=): Final value of a is 0

Next Lesson

  • Topic: Ternary Operators

Conclusion

  • Reminder: Subscribe for more videos and lessons

Thank you for attending this lecture.