Lesson four operators, expressions in C + +

Source: Internet
Author: User
Tags bitwise bitwise operators

An operator is a noun that is used to calculate some symbols, and an expression is composed of statements and operators. These are expressions, such as x=5, or x=a+b. First, explain the operator.

Operators can be divided into assignment operators, mathematical operators, relational operators. The assignment operator is represented by (=). As x= 1 means assigning a value of 1 to X instead of being equal in mathematics, it must be clearly distinguished.

The math operator is our usual +,-, *,/,%. There are also self-increment operations in C + +, and self-decrement calculations, such as I++,i--,++i,--I, which are often easily mistaken, especially when confronted with more complex expressions. ++i before also called the former self-increment, Gu Mings meaning is to increment the value of I first, let after the assignment, and i++ is the opposite, the first assignment, and then increment. To illustrate:

1#include"stdafx.h"2 3#include <iostream>4 using namespacestd;5 intMainintargcChar*argv[])6 {7     inti =Ten;8     intj =Ten;9cout<<"i ="<<i<<Endl;Tencout <<"J ="<<j<<Endl; Onei++; A++J; -cout<<"i ="<<i<<Endl; -cout<<"j ="<<j<<Endl; thecout<<"i++="<<i++<<Endl; -cout<<"++j="<<++j<<Endl; -      -     return 0; +}

Return results we can clearly see the results of 13, 14 rows. The 15 row result is 11, and the 16 row is 12. This shows that the former self-increment is the first addition to the assignment.

  Relational operators

A relational operator is a comparison of two numbers. Common are =, <, >, <=, >=,! =

Name Operator Example Results
Equals == 1==2 False
Not equal to != 1!=2 True
Greater than > 1>2 False
Less than < 1<2 True
Greater than or equal >= 1>=2 False
Less than or equal <= 1<=2 True

  logical operators 

Logical operators include! (non), && (with), | | (or), the priority is reduced in turn. Using a logical operator to connect a relational expression to a logical expression, the result of a logical expression is also a bool type, and the value can only be true or false. "!" is a unary operator, using the form Yes! operand. A non-operation is the negation of the operand. For example, if the value of!a,a is true, the value of!a is false. "&&" is a two-dollar operator, which is used to calculate the logic of two operands, and only two operands are true, the logical and result is true, and the result is false in other cases. "||" is also a two-dollar operator, which is used to calculate the logic of two operands, or if only two operands have a value of false, the logical or result is false, and in other cases the result is true. For example, int a=3,b=5,c=2,d=1; The value of the logical expression (a>b) && (c>d) is false.

  sizeof operator

The sizeof operator is used to calculate the number of bytes that an object occupies in memory. This operator is used in the form of sizeof (type name) or sizeof (an expression). The result of the calculation is the number of bytes in memory of this type or the result of the expression.

Trinocular operator

  Is the expression 1? Expression 2: expression 3 means:

If the value of expression 1 is true, the value of expression 2 is returned, and if False, the value of expression 3 is returned.

Example

if (1>2)? 1:2;

A result of 2 returns the value of expression 3.

Bitwise operators

Bitwise AND (&). It is the logic and operation of each bit in the binary form of the two operands. For example, the binary form of 3 for the 00000011,5 binary form is 00000101, the bitwise AND after the result is 00000001.

Bitwise OR (|). It logically or operates on each of the two binary forms of the two operands. Or, for example, 3 and 5, the result is 00000111 after a bitwise OR operation.

Bitwise XOR or (^). It makes a difference to each of the two operands, that is, if the corresponding bit is the same, the result of the operation is 0, if the corresponding bit is different then the result is 1. For example, 3 and 5 bitwise XOR and the result is 00000110.

Bitwise inverse (~). This is a unary operator. It is the negation of each bit of a binary number. For example, 3 bitwise inversion is 11111100.

Shift. Includes the left shift operation (<<) and the right Shift operation (>>), all of which are two-dollar operators. The number to the left of the shift operator is the value to shift, and the number to the right is the number of bits moved. Shift left shifts the binary value of a number to the left by the specified number of digits, and after the left, the low is 0, and the moved high is discarded. Shift right shifts the binary value of a number to the right by the specified number of digits, moves the lower position after the right shift, and if the unsigned number is a high 0, or 0 if it is a signed number, the general complement sign bit. For example, the value of a char variable is-8, then its binary complement value in memory is 11111000, so a>>2 needs to move the rightmost two 0 out, the leftmost two 1, because the sign bit is 1, the result is 11111110, and then the complement to the final result-2.

Lesson four operators, expressions in C + +

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.