0% found this document useful (0 votes)
4 views3 pages

Arithmetic Operators

The document outlines various types of operators used in programming, including arithmetic, comparison, logical, assignment, and bitwise operators. Each operator is described with its function, examples, and the expected result type. This serves as a reference for understanding how to perform mathematical calculations, comparisons, and logical operations in code.

Uploaded by

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

Arithmetic Operators

The document outlines various types of operators used in programming, including arithmetic, comparison, logical, assignment, and bitwise operators. Each operator is described with its function, examples, and the expected result type. This serves as a reference for understanding how to perform mathematical calculations, comparisons, and logical operations in code.

Uploaded by

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

.

Arithmetic Operators
Used to perform common mathematical calculations.
Operator Description Result 6 Examples
Range/Type

Addition (+) Adds two values Sum of operands 5 + 2 , x + y , -1 + 10 , 3.5 +


together. 2 , 0 + 0 , 100 + 1

Subtraction (-) Subtracts one value Difference of 10 - 5 , 0 - 8 , x - y , 5.5 -


from another. operands 2.1 , -5 - -5 , 100 - 1

Multiplication Multiplies two values. Product of 3 * 3 , -2 * 4 , 0 * 10 , x *


(*) operands y , 0.5 * 2 , 10 * 10

Division (/) Divides one value by Quotient 10 / 2 , 1 / 2 , 22 / 7 , x /


another. (Float/Int) y , 100 / 10 , -10 / 2

Modulus (%) Returns the division 0 to (Divisor - 1) 10 % 3 , 5 % 2 , 9 % 3 , x %


remainder. 2 , 100 % 10 , 1 % 2

Exponent ( ** or Raises a number to a 2 ** 3 , 10 ** 2 , 5 ** 0 , x


^) power. ** y , 4 ** 0.5 , 3 ** 2

2. Comparison (Relational) Operators


Used to compare two values. They always return a Boolean (True/False).
Operator Description Result Range 6 Examples

Equal (==) True if values are True / False 5 == 5 , x == 10 , "A" ==


equal. "A" , True == True , 0 == 0 , y ==
z

Not Equal (!=) True if values are True / False 5 != 3 , x != y , "A" != "B" , true
not equal. != false , 0 != 1 , 10 != 5

Greater Than True if left is greater True / False 10 > 5 , x > 0 , -1 > -5 , 100 >
(>) than right. 1 , 0.5 > 0.1 , y > z

Less Than (<) True if left is less True / False 3 < 10 , x < y , -5 < 0 , 1 <
than right. 100 , 0.1 < 0.5 , z < 10

Greater or True if left is greater True / False 5 >= 5 , 10 >= 2 , x >= y , 0 >= -
Equal (>=) or equal. 1 , 100 >= 100 , 1 >= 0

Less or Equal True if left is less or True / False 5 <= 5 , 2 <= 10 , x <= 0 , -1 <=
(<=) equal. 0 , 0 <= 0 , y <= z

3. Logical Operators
Used to combine conditional statements.
Operator Description Result Range 6 Examples

AND True if both True / False T and T , (5>3) and (1<2) , x and y , T and
(&& / statements F , F and F , 1 and 1
and) are true.

OR (|| / True if at True / False T or F , F or F , (x==1) or (y==2) , T or T , 0


or) least one is or 1 , F or T
true.

NOT (! / Reverses True / False not True , !False , !(x==y) , not(5>2) , !


not) the logical True , not False
state.

4. Assignment Operators
Used to assign values to variables.
Operator Description Usage 6 Examples

Simple (=) Assigns right side to x = y x = 5 , name = "AI" , y = x , z = 0 , a =


left side. 10 , b = True

Add & Assign Adds then assigns. x = x x += 1 , count += 5 , price += tax , i


(+=) + y += 1 , total += sub , a += b

Sub & Assign Subtracts then x = x x -= 1 , stock -= 10 , health -= 5 , i -


(-=) assigns. - y = 1 , bal -= 20 , a -= b
Mult & Assign Multiplies then x = x x *= 2 , total *= 1.1 , i *= 10 , y *=
(*=) assigns. * y 0.5 , a *= a , val *= 3

Div & Assign Divides then x = x /= 2 , half /= 2 , total /=


(/=) assigns. x / y items , i /= 10 , a /= b , z /= 5

Mod & Assign Takes modulus then x = x x %= 2 , val %= 10 , i %= 3 , a %=


(%=) assigns. % y b , num %= 5 , y %= 4

5. Bitwise Operators
Used to perform operations on binary bits (

and

).
Operator Description Level 6 Examples

AND (&) Sets bit to 1 if both bits Bit-by- 5 & 1 , x & y , 0 & 0 , 1 & 1 , 255 &
are 1. bit 0 , 10 & 7

OR (|) Sets bit to 1 if one of two Bit-by- 5 | 1 , x | y , 0 | 1 , 1 | 1 , 255 | 0 , 8


bits is 1. bit | 2

XOR (^) Sets bit to 1 if only one Bit-by- 5 ^ 1 , x ^ y , 1 ^ 1 , 0 ^ 0 , 10 ^ 3 , 12


bit is 1. bit ^ 4

NOT (~) Inverts all the bits. Bit-by- ~5 , ~0 , ~x , ~1 , ~255 , ~-1


bit

Left Shift Shifts bits to the left. Bit-by- 5 << 1 , x << 2 , 1 << 8 , 10 << 1 , 2 <<
(<<) bit 3 , 4 << 2

Right Shift Shifts bits to the right. Bit-by- 5 >> 1 , x >> 2 , 10 >> 1 , 256 >> 8 , 8
(>>) bit >> 2 , 1 >> 1

You might also like