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 use after change" such as i--;

For example:

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

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


(2) There is a difference between the front and 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 backend returns a temporary variable.

int Mian () {int a = 3;a + = (++a);       a=7a + = (a++);       A=16 (++a) + = (a++);    A=35 (a++) + = A;   Not correct}

2. Relational and logical operators

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

(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 the 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, the binary number that corresponds to 52 is: 110100.

< Span style= "COLOR: #444444" > negative integers are converted to binary : Take the inverse plus one

Explanation: The negative integer corresponding to the positive integer is first converted to binary, and then the "Fetch", and then add 1 to the result of the complement.

For example, to convert-52 into binary:

1. Get 52 binary First: 00110100

2. Inverse of the resulting binary number: 11001011

< Span style= "COLOR: #444444" >3. Add an inverse value by 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 5*16^1+3*16^0 in this form .


(3) Shift operation

Rule: After the shift, discard the number of moved out, after the space-shift after the following rule:

int left shift low 0, right shift high fill sign bit

unsighed int Low High 0


4. Type conversion

(1) Assignment conversion: Assigns a value of one type to a variable of another type.

int a = 3.14;

(2) Expression conversion

A, the integral type promotion

b, Operation time conversion

The same type 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 the other binocular

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

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

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.