Objective-C: Magic path [4-operators and expressions]

Source: Internet
Author: User
Tags bitwise operators

Do not repost the labor results of developers


Operators and expressions

Operators can be divided into the following types:
? Arithmetic Operators, +,-, *,/, %, ++ ,――
? Relational operators,>, <, >=, <=, = ,! =
? Boolean logical operator ,! , &, |
? Bitwise operator, &, |, ^ ,~ , >>,<
? Value assignment operator, + =,-=, * =,/=
? Conditional operator ,? :


Short and short or

There are two Special operators in the Boolean logical operators, "&" and "|", where "&" is short-circuit and if two expressions are calculated, if the value of the 1st expressions is "false", it is irrelevant to the value of the 2nd expressions and the result is "false". Therefore, the 2nd expressions are not calculated at this time. "|" Is short circuit or. If two expressions are calculated, if the value of the 1st expressions is "true", it is irrelevant to the value of the 2nd expressions, the result must be "true", so the 2nd expressions are not calculated at this time.
Example: int I = 0;
Int A = 10;
Int B = 9;
If (A> B) | (I ++ = 1 )){
Nslog (@ "A> B ");
} Else {
Nslog (@ "A <B ");
}
Nslog (@ "I = % I", I );

The result is A> B and I = 1.

Bitwise operators

Bitwise operators include the following operators: &, |, ^ ,~ , >>,<, Where & is the bitwise AND, | is the bitwise OR, ^ is the XOR ,~ Is the inverse,> is the right displacement, <is the left displacement.

Bitwise operator example:
Assume that there are two 16-bit integers (short INT) in binary ),
If a = 1001110110011101 and B = 0011100100111001, the following results are displayed:


Conditional Operators

The syntax format of conditional operators is:
Boolean expression? Return Value 1: Return Value 2
If the value of a Boolean expression is true, the value of expression 1 is returned. Otherwise, the value of expression 2 is returned. Example:
Int I = 70;
Int I3 = 70
Nsstring * res = I3> 60? @ "Pass": @ "fail ";
Nslog (@ "res = I3> 60% @", Res );
The result is "pass"

Expression
Expressions are composed of numbers, operators, numerical grouping symbols (parentheses), free variables, and constraint variables.
A combination of meaningful permutation methods that can obtain values.
The constraint variable has been specified in the expression, while the free variable can be specified outside the expression.


Expression meaning
Some numeric values are given to the free variable, and a numeric value can be given to the expression. Even for some values of the free variable, the expression may not be defined. Therefore, an expression represents a function. Its input is the value of a free variable, and its output is the value generated after the expression is generated.


For example, the expression x/y sets the values of free variables X and Y to 10 and 5 respectively, and the output value is number 2, but is not defined when the value of Y is 0.
The assignment of an expression is associated with the definition of the operator and the definition of the value.
If two expressions are equivalent, they indicate that the two expressions have the same output for any value of the free variable, that is, they represent the same function.
An expression must be a combination. That is, each operator must have a correct input number, where it is. For example, expression 2 + 3 is a combination, and expression * 2 + is not a combination, at least not a general markup of arithmetic.
Expressions and their assignments were formulated in their lambda calculus by arenzo Qiu Qi and Stephen Kleene in 1930s. Lambda calculus has had a significant impact on the development of modern mathematics and computer programming languages.


Expression classification
1. arithmetic expression:
Arithmetic expressions are the most common expressions, also known as numerical expressions. It is a mathematical formula used for arithmetic operations.
Arithmetic Operators
1) multiplication operator *
X * Y: 6*7 = 42
2) Division operator/
For example, if x/y is used to calculate the value of X except y (floating point calculation), 2.76/1.2 = 2.3
3) Division operator Div
X Div Y: calculate the integer quotient of X except y (for integer calculation). Example: 25 = 5
4) mod
X mod y calculates the remainder of X except y (for integer calculation). Example: 25 mod 4 = 1
5) Addition operator +
Example of addition operation X + Y: 32 + 2 = 34
6) subtraction operator-
-Example of X-Y subtraction: 48-21 = 27


2. logical expressions
There are only two logical operations: true and false ). OC provides six Relational operators and three logical operators:
= (Equal to), <(less than), <= (less than or equal to),> (greater than), >=( greater than or equal ),! = (Not equal)
! (Not), & (and), | (OR)


Expression operation Priority
During expression conversion, you must know the priority of each operation so that the converted expression can meet the operation requirements of the mathematical formula. The operation Priority is:
Parentheses → functions → multiplication, division → addition, subtraction → character Concatenation Operators → Relational operators → logical operators
If the operation at the same level is performed in the left-to-right order, the multi-layer brackets are left-to-right.
Example:
(10 + 6) * 3 ^ 2 * Cos (1)/2*8 + 7
① ③ ⑤ ⑥ 7th
SQRT (ABS (P/N-1) + 1
④ ① ② ⑤

Objective-C: Magic path [4-operators and expressions]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.