Operations Flashcards
++ / - - operator
++variable
Variable++
- - variable
Variable- -
+1 , -1 to variable value
Relative to order with variable
I.e number++»_space; prints the number then adds 1
~
Bitwise complement
Flips value of bitwise
1111»_space; 0000
!
Logical negation
Inverts Boolean logic
True»_space; false
\+ - * / %
Addition Subtraction Multiplication Division Modulus
Modulus»_space; remainder
What is the modulas
Modulus is just the remainder
So 6/3 is 0 but 5/2 is 5
==
!=
Equal to
Not equal to
<
>
<=
>=
Less than
Greater than
Less than or equal to
Greater than or equal to
&&
||
!
&& = And»_space; result will be true if both expressions are true
Int num1 = 6;
Int num2 = 5;
Print(Num1 < 10 && num2 < 5);
Output = false
|| = Or»_space; result will be true if any expression is correct
Print(Num1 < 10 || num2 < 5);
Output = true
! = NOT»_space; result will be false if the expression is true and vice versa
Logic table
A B A&&B A||B. A !A
True true True
True false. False
False true
False false
A B, A&&B, A||B. A !A True true true true True false True false false true False true False true false true False false false false
Ternary operator
Single line replacement for
If-then-else statements and acts upon three operands
? : ;
Assignment operators = \+= -= *= /=
= assigns value on right to variable on left
+= adds the current value of variable on left to value on right and returns result to variable on the left
-= subtracts current value of variable —“—
*= multiplies current value of variable —“—
/= divides current value of variable —“—
Bitwise counting
Bitwise most likely won’t be tested
Manipulate the individual bits of a number
Recall: 11001 = 2^4+ 2^3 + 2^0
Last digit is 2^0 and the power increases as you go left, if 0: spot is false, if 1: spot is true
Bitwise operators
Bitwise OR: | if either bit is 1 then it
gives 1 if not zero
10|5 (1010|0101) = 1111 or 15
Bitwise AND: & If both bits are 1 then it gives 1 if not 0
10&5 (1010&0101) = 0000 or P
Left shift operator: «_space;left shifts the bits of the first operand and the amount shifted is by the second operand (fills spot with zero)
I.e 10«1
1010 becomes 10100 which is 20
Right shift operator:»_space; opposite of left shift, fills 0 if positive operand or 1 if negative operand.
10»1
1010 becomes 0101 which is 5
Unsigned right shift:»_space;> right shift operator but no correction for negative numbers
Precedence (order) of operators
Not too important to know specifics, just a vague generalisation
1) Brackets
2) postfix (++.- -)
3) prefix, unary (++, - -, +,-,~,!)
4) multiplication (,/,%)
5) additive (+,-)
6) shift (<>,»>)
7) relational (,<=,>=)
8) equality (==,!=)
9) logicaL AND (&&)
10) logical OR (||)
11) ternary ?:
12) assignment (=,+=,-=,=,«=,»=,»>=)
Logical order and bitwise order
Logical operators are lazy and won’t check the second statement if first statement is false, so can affect any operation performed in statement
Difference between bitwise or and logical or and bitwise AND and logical AND