"C-language" symbol precedence

Source: Internet
Author: User
Tags bitwise

I. The problem arises

Today to see Ali's pen test, see a very interesting topic, but it is easy to make mistakes.

  Title: The following function, the value of the 32bit system foo (2^31-3) is:

int foo (int  x) {return x&-x;}

 Answer: If you want to correct this problem, first of all to understand the C language of the priority level of the symbol, minus (-) the priority is higher than ^, so 2^31-3=2^28, there is a trap is C language that ^ is an XOR operation instead of a power function, so 2^28=30, and then calculate 30 &-30 results. And because the data in computer memory is in the form of twos complement, the number of participating bit operations is in the form of complement. So we need to convert 30 and 30 to complement, then bitwise AND operation, the result is 2. We can also use the program to see the value of X generated during execution as follows:

#include<iostream>using namespacestd;intFoointx) {cout<<"x ="<< x <<Endl; returnX &-x;}voidMain () {intres =0; Res= Foo (2^ to-3); cout<<"res ="<< Res <<Endl;}
Two. Priority of characters

Priority level

Operator

Name or meaning

Use form

Combination direction

Description

1

[]

Array subscript

array name [constant expression]

Left to right

()

Parentheses

(expression)/function name (formal parameter list)

.

Member selection (object)

Object. Member name

-

Member selection (pointer)

Object pointers, member names

2

-

Minus sign operator

-expression

Right to Left

Monocular operator

Type

Forcing type conversions

(data type) expression

++

Self-increment operator

+ + variable name/variable name + +

Monocular operator

--

Self-decrement operator

--Variable name/variable name--

Monocular operator

*

Value operator

* Pointer variable

Monocular operator

&

Fetch address operator

& Variable Name

Monocular operator

!

Logical non-operator

! An expression

Monocular operator

~

Bitwise inverse operator

~ Expression

Monocular operator

sizeof

Length operator

sizeof (expression)

3

/

Except

An expression/expression

Left to right

Binocular operator

*

By

Expression-expression *

Binocular operator

%

Remainder (modulo)

Integer expression/integer expression

Binocular operator

4

+

Add

An expression + an expression

Left to right

Binocular operator

-

Reducing

Expression-expression

Binocular operator

5

<<

Move left

Variables << expressions

Left to right

Binocular operator

>>

Move right

Variables >> expressions

Binocular operator

6

>

Greater than

Expressions > expressions

Left to right

Binocular operator

>=

Greater than or equal

Expression->= expression

Binocular operator

<

Less than

Expressions < expressions

Binocular operator

<=

Less than or equal

Expression-<= expression

Binocular operator

7

==

Equals

Expression-= = Expression

Left to right

Binocular operator

!=

Not equal to

Expression! = Expression

Binocular operator

8

&

Bitwise-AND

Expressions & Expressions

Left to right

Binocular operator

9

^

Bitwise XOR OR

An expression ^ expression

Left to right

Binocular operator

10

|

Bitwise OR

Expression-expression

Left to right

Binocular operator

11

&&

Logic and

Expressions && Expressions

Left to right

Binocular operator

12

||

Logical OR

An expression | | An expression

Left to right

Binocular operator

13

?:

Conditional operators

Expression 1? Expression 2: Expression 3

Right to Left

Trinocular operator

14

=

Assignment operators

variable = expression

Right to Left

/=

Assign value after addition

Variable-/= expression

*=

Multiply post-Assign value

Variable-*= expression

%=

Assign value after modulo

Variable-%= expression

+=

Add after Assignment

Variable + = expression

-=

Reduced-value Assignment

Variable-= expression

<<=

Assign value after left shift

Variable-<<= expression

>>=

Assign value after right shift

Variable->>= expression

&=

Bitwise AND post-assigned values

Variable-&= expression

^=

Bitwise XOR or post-assignment

Variable-^= expression

|=

Bitwise OR post-assigned value

Variable-|= expression

15

,

Comma operator

expression, expression,...

Left to right

Left-to-right sequential operation

Description: operators of the same precedence, the order of operations is determined by the binding direction.

Three. Priority formula

parenthesis member first; parentheses operator [] () member operator. -

All monocular second; All monocular operators such as + + + (positive)-(negative) pointer operation *&

Multiplication Yozo, plus minus four; this "remainder" means the residual operation is%

Shift five, relationship six, shift operator:<< >>, relationship:> < >= <=, etc.

Equals (and) unequal row seventh; = = = = =

Bits and Xor and bits or; these are bitwise operations: Bits and (&) XOR (^) bits or (|)

"Three points of the world" eighty or ninety;

logical or heel; logical Operator: | | and &&

12 and 11; Note order: Priority (| |) Bottom-to-priority (&&)

The condition is higher than the assignment, the three-mesh operator precedence is ranked to 13-bit only than the assignment operator and the "," high

Lowest comma operation level! The comma operator has the lowest precedence

Resources

[1] http://blog.chinaunix.net/uid-23577393-id-2733234.html

[2] http://blog.csdn.net/zhlfox2006/article/details/11854799

[3] Http://www.aichengxu.com/article/c%E8%AF%AD%E8%A8%80/31501_11.html

"C-language" symbol 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.