Home > Back-end >  C (2)
C (2)

Time:11-08

Various operators


1, the comma operator

Use of the comma operator is to put a few expression together, the entire value of the comma expression for the value of the last expression in the series,



2, the relational operators

Equal operator (==), ranging from operator (!=), or more than operators (& gt;) , less than operators (& lt;) , greater than or equal to operator (& gt;=) and less than or equal to operator (& lt;=),

(unless the overloading, the result of relational operators are generally booleans)



3, the conditional operator
Form: exp1? Exp2: exp3
If exp1 is true, then calculating the value of exp2, and for the final result; If exp1 is false, then calculating the value of exp3, and for the final result,


4, logical operators

Including the logical and operator (& amp; &) , the logical or operator (| |) and logical not operator (!) ,

The logic and logic or operations in c + + USES short circuit implementation, namely for (A& & B), if have operations that A is false, no longer to operation of B, direct return result false; For (A | | B), if have operations that A is true, is no longer to operation of B, direct return true,

The sizeof
Definition:
The sizeof operator returns an expression or a type name of the number of bytes,
Sizeof meet right associative law, the value of their income is a constant expression of type size_t, (type size_t unsigned types is a kind of machine, large enough can represent arbitrary objects in memory size)
Method of use:
Sizeof (type);
Sizeof expr.

Note:
Sizeof result partly depends on the type of the role of:
1, the expression of char or type is char implement sizeof, the result is 1;
2, implementation for reference types, get be accounted for by reference object space size;
3, to perform the sizeof pointer to get the pointer itself accounts for the size of the space;
4, to perform get pointer dereferencing a pointer pointing to the object of space, the size of the pointer does not need effective;
5, to carry out to get the whole array of space array size, equivalent to an array of all the elements to perform a sizeof and will get the result of the sum,

An operator
Bit operator ACTS on the bit, and perform bitwise operations, & amp; | and ^ truth table as shown below:
P q p & amp; Q p | q p ^ q
0 0 0 0 0
0 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume that if A=60, and B=13, expressed as A binary format now, they are as follows:
A=0011 1100
B=0000, 1101
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
A& B=0000, 1100
A | B=0011, 1101
A ^ B=0011, 0001
~ A=1100 0011
The table below shows the c + + support operator, assuming that the value of the variable A is 60, variable has A value of B 13, is:
Operator describe instance
& If at the same time, exists in two operands, binary AND copying an operator to results, (A & amp; B) will be 12, that is, 0000 1100
| if exists in any one of the operands, binary OR copying an operator to results, (A | B) will be 61, 0011, 1101
^ if exists in one of the operands but not at the same time exists in two operands, copying A binary xor operator to results, 49 (A ^ B) will be, that is, 0011 0001
~ two's complement operator is A unary operator, has A "flip" effect, namely 0 to 1, 1 to 0, (to A) will be - 61, to 1100, 0011, A symbol of the binary number complement form,
> Binary right shift operator, the value of the left-hand operand to the right right operand specified digits, a. & gt;> 2 will be 15, that is, 0000 1111
Operator precedence
Associative law priority operator mnemonic
1: : from left to right scope
2 +, -, a type (), type {}, a (), [], a., & gt; From left to right suffix since the increase and decrease, transformation function style, function calls, subscript, member access
3! , ~ + + a, a, a + a, - a, (type), sizeof, & amp; A, * a, new, new [], delete, delete [] than from right to left logic, bitwise not, prefix the increase and decrease, plus or minus, C style transformation, size, address pointer, dynamic memory allocation
4. * - & gt; * from left to right pointer to member
5 a * b, a/b, from left to right, a % b, modulus
6 a + b, a - b from left to right and subtract
7 & lt; <, & gt;> From left to right around the bitwise
8 & lt; ,<=, & gt; , & gt;=from left to right size comparison
9==,!=from left to right is equivalent to compare
10 a& From left to right the bitwise and b
11 ^ from left to right the bitwise xor
12 | from left to right bitwise or
13 & amp; & From left to right logic and
14 | | from left to right logic or
15 a? B: c,=, +=-=, *=/=%=, & amp;=^=, |=, & lt; <=, & gt;>=three yuan conditions from right to left, assignment
16, from left to right the comma
Data type conversion
One, in the c + + data type conversion is generally has the following several ways:
1, will be a kind of arithmetic types of value is assigned to the other arithmetic types of variables, c + + to convert the value
2, contains different types of expression, the c + + to convert the value
3, the parameter passed to the function, the c + + to convert the value
Two, when carries on the data type conversion to pay attention to:
1, big small data types can be converted to the type of data, there is no problem, just after the conversion of bytes occupied more commonly, but to big data types into small data type, can cause data loss,
2, large floating point number into smaller floating-point number, such as double into float, which can cause (digital) effectively reduce accuracy, value may be beyond the scope of the target type, in this case, the result will be uncertain,
3, converts floating-point type to integer, which can cause the decimal part is missing, the value of the original may be beyond the scope of the target type, in this case, the result will be uncertain,
4, when the operation involves two kinds of data type, are generally smaller types will be converted to a larger type,
Three, casts:
1, form:
A, (typeName) value written (C)
B, the value (typeName) (c + + written)
C, static_cast & lt;> Can convert values from one numeric type to another data type, format for: static_cast & lt; TypeName> (value)
Four, conclusion:
The integer data type size sorting: bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long
Floating point number ordering size: float, double, long double

CodePudding user response:

Summary is very good, the good things
  • Related