C + + operators and their precedence

Source: Internet
Author: User
Tags decimal to hex

1, Self-increase self-reduction

(1) pre-operation: "first Change after use" such as ++i.

Post-operation: "first change after" such as i--。

Like what:

int i = 5. y1 = ++i;   y2 = i++;

Result: y1 = 6;y2=6;i=7.


(2) Another difference between the front and the back: the front can be used on both sides of the equal sign, and the rear can only be on the right side of the equal SIGN.

This is because the predecessor returns the operand itself, and the back is a transient variable.

int Mian () {int a = 3;a + = (++a);       a=7a + = (a++);       A=16 (++a) + = (a++);    a=35 (a++) + = a;   Wrong

2. Relational and logical operators

(1) relational operators (four types) and logical operators (three): returns the result of a Boolean type

Relational operators, such as:< (less than),> (greater than), <= (less than or equal to), >= (greater than or equal to),! = (not equal to)

Logical operators, such as:! (non),&& (and), | | Or

(2) precedence > logical operators for Relationships


3, bit arithmetic

(1) A total of six kinds of:&, |, ^, ~, "(shift left)," (move right)

XOR ^: The result is 1 only if two bits are Different.


(2) binary Conversion

[decimal Turn binary]

positive decimal to binary: in addition to the second Take-back sort. A direct addition to the quotient equals 1 or 0 o'clock.

So. 52 the corresponding binary number is: 110100.

negative integer converted to binary : take inverse plus one

Explanation: the negative integer corresponding to the positive integer is first converted to binary, and then the "take up", and then the result after the addition of 1 can Be.

For example, to convert-52 into binary:

1. Get 52 binary first: 00110100

2. Inverse of the resulting binary number: 11001011

3. Add the inverse value to one: 11001100

namely:(-52)ten= (11001100)2


[decimal to Hex]: In addition to 16, the order of the remainder is Inverted.

representation of hexadecimal number: prefix method: 0xa1

Suffix method: a1h


[binary and hexadecimal to decimal]

The same algorithm.

It's all in the form of 5*16^1+3*16^0 .


(3) shift operation

Rule: after the shift, discard the number of moves out, and after the shift the space complement rules such as the Following:

int left shift Low 0. Move right to high fill sign bit

unsighed int Low High 0


4. Type Conversion

(1) Assignment Conversions: assigns a value of one type to a variable of one type.

int a = 3.14.

(2) Expression Conversion

a, the integral type promotion

b, Operation time Conversion

The same types of signed and unsigned types occupy the same memory space.

(3) Display Conversion

(4) Other conversions


5, Operation Priority

(1) brackets , subscripts, and struct members have the highest precedence

(2) The monocular is higher than the binocular, the arithmetic binocular is higher than other binocular

(3) arithmetic > Shift > Relationship > Bit operation > Logic

(4) Three mesh operation only has a comma, lower than the logical

(5) The value of the assignment is higher than "," .

C + + operators and their precedence

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.