Use of Operators

Source: Internet
Author: User
Tags bitwise operators random seed

Use of Operators

Arithmetic Operators

Arithmetic Operator function list

Arithmetic Operators

Description

Arithmetic Operators

Description

+

Addition Operator

/

Division Operator

-

Subtraction Operator

%

Modulo Operators

*

Multiplication Operator

 

 

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int result = 1; int I = 0; int count = 0; char Text [128]; char cryptograph [128]; while (1) {if (result = 1) {printf ("Enter the plaintext to be encrypted: \ n "); scanf_s (" % s ", & Text, 128); count = strlen (Text); for (I = 0; I <count; I ++) {cryptograph [I] = Text [I] + I + 5;} cryptograph [I] = '\ 0'; printf ("the encrypted ciphertext is: % s \ n ", cryptograph);} else if (result = 2) {count = strlen (cryptograph); for (I = 0; I <count; I ++) {Text [I] = cryptograph [I]-I-5;} Text [I] = '\ 0'; printf ("the decrypted plaintext is: % s \ n ", Text);} else if (result = 3) {break;} else {printf (" enter the correct command: \ n ");} printf ("Enter 1 secret New plaintext, enter 2 to decrypt the encrypted ciphertext, enter 3 to exit the system: \ n"); printf ("Enter the command character: \ n "); scanf_s (" % d ", & result);} return 0 ;}

 

Bitwise operators

In a computer, data is represented in binary format and stored in bytes as the minimum unit. One byte is divided into eight bits, each of which can represent a binary number of 0 or 1. To operate one or several bits in a byte, C ++ provides six bitwise operators.

 

Bitwise operator table

Bitwise operators

Name

Description


&


Bitwise AND OPERATION

When two binary bits are bitwise AND computed, if both binary bits are 1, the result is 1. If at least one binary bit is 0, the result is 0.

|

Bitwise OR

When two binary bits are used for or operations, if one binary bit is 1, the result is 1. If both binary bits are 0, the result is 0.

^

Bitwise OR

When bitwise exclusive or operation is performed, the result is 0 if two binary numbers are the same. Otherwise, the result is 1.

~

Bitwise Inversion

Inverse Operator ~ Returns the bitwise inverse of a binary number, which converts 0 to 1 and 1 to 0.


<


Left shift operation

The left-shift operation is to move a binary operand object to the left Based on the specified number of digits. The overflow bits on the left (high end) are discarded, and the vacancy on the right (low end) is supplemented with 0. The power multiplied by 2.

>

Right Shift operation

The right shift operator is opposite to the Left shift operator. It shifts the binary digits of a number to several places and adds 0 to the left.

 

Note: When performing the right shift, pay attention to the symbol bit problem for the number of symbols. When the value is an integer, the maximum bit is 0, while the value is negative, whether the maximum bit is 0 or 1 depends on the requirements of the compilation system.

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int iVar = 18; int jVar = 10; cout <"pre-conversion iVar =" <iVar <"\ n"; cout <"pre-conversion jVar =" <jVar <"\ n "; iVar = iVar ^ jVar; jVar = iVar ^ jVar; iVar = jVar ^ iVar; cout <"converted iVar =" <iVar <"\ n "; cout <"after conversion jVar =" <jVar <"\ n"; int a = 2, B = 4, c = 6, d = 8; unsigned result; result = a & c; Printf ("a & c = % u", result); result = B | d; printf ("\ nb | d = % u", result ); result = a ^ d; printf ("\ na ^ d = % u", result); result = ~ A; printf ("\ n ~ A = % u ", result); system (" Pause "); return 0 ;}

 

Three-object meta Operator

In C ++, a three-object meta expression is a unique three-object meta operator "? : ". This operator is called a conditional operator, which requires three operand objects. The general form of this operator is as follows:

Expression 1? Expression 2: expression 3

The execution sequence of conditional operators is as follows: first obtain the value of expression 1. If the value is true, then solve expression 2 and use the value of expression 2 as the value of the entire three-object element expression; if the value of expression 1 is false, expression 3 is solved and the value of expression 3 is used as the value of the entire three-object element expression.

 

Note:

When you use the three-object meta operator, conditions are usually enclosed in parentheses. In fact, it can be left blank, because the priority of the Three-object meta operator is relatively low. During calculation, the condition section is calculated first. At the same time, the priority of the three orders element operator is higher than that of the value assignment operator, which is lower than that of the relational operator and arithmetic operator, so it can be used even without parentheses.

The Union direction of conditional operators is "from right to left ".

In conditional expressions, expression 1 can be of different types than expression 2 and expression 3.

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {char name [6]; int sex; printf ("Enter name: \ n"); scanf_s ("% s", name, 6); printf ("Enter 1 or for male, 0 for female: \ n "); scanf_s (" % d ", & sex); printf (" name: % s \ n ", name ); char * strSex = (sex = 1 )? "Male": "female"; printf ("Gender: % s \ n", strSex); system ("Pause"); return 0 ;}

 

Define macros with Parameters

Macro Syntax:

# Define macro name (parameter table) String

Note:

(1) To expand a macro with parameters, simply replace the real parameter string following the macro name in the statement with the form parameter in the # define command line.

(2) when defining a macro, spaces cannot be added between the macro name and the brackets with parameters. Otherwise, the characters After spaces are used as part of the replacement string.

(3) In the macro definition with parameters, the formal parameters do not allocate memory units, so you do not need to define the type.

(4) The macro definition replaces the string with the macro name, but does not check the correctness.

(5) The macro definition does not need to add points at the end of the row.

(6) # The define command appears outside the function of the program. The valid range of the macro name is to define the command and end with the source file.

(7) You can use the # undef command to terminate the macro-defined scope.

(8) When defining a macro, You can reference the defined macro name and replace it with another one.

(9) The characters in the string enclosed by double quotation marks in the program are not replaced.

 

# Include "stdafx. h "# include <iostream> # define swap (a, B) {int c; c = a; a = B; B = c;} using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int I, a [10], B [10]; printf ("Please input 10 numbers to array: \ n "); for (I = 0; I <10; I ++) {scanf_s (" % d ", & a [I]);} printf ("display array a: \ n"); for (I = 0; I <10; I ++) {printf ("% d ,", a [I]);} printf ("Enter 10 numbers to array B: \ n"); for (I = 0; I <10; I ++) {scanf_s ("% d", & B [I]);} printf ("\ n display array B: \ n"); for (I = 0; I <10; I ++) {printf ("% d,", B [I]) ;}for (I = 0; I <10; I ++) swap (a [I], B [I]); printf ("\ n output converted array a: \ n"); for (I = 0; I <10; I ++) {printf ("% d,", a [I]);} printf ("\ n output converted array B: \ n "); for (I = 0; I <10; I ++) {printf ("% d,", B [I]);} system ("Pause "); return 0 ;}

 

Rand ()

Rand () is used in the program to generate a random number and return this number, a = rand () % max; the specific meaning is to generate any random number within max (excluding max itself ).

 

Set Random Seed

In order to run the same program each time, the random sequence is not the same, set the seed by system time, that is, srand (unsigned long) time (0 )). # Include "time. h" must be referenced ".

 

# Include "stdafx. h "# include <iostream> # include" time. h "# include" conio. h "using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int a, B, c, sign, max; char sign1; printf ("Enter the operator (1 or another number, 1 represents:-, other digits represent: +): \ n"); scanf_s ("% d", & sign ); printf ("Enter the maximum range (<1000): \ n"); scanf_s ("% d", & max); srand (unsigned long) time (0); a = rand () % max; B = rand () % max; while (a <B) & (sign = 1 )) {a = rand () % ma X; B = rand () % max;} sign1 = (sign = 1? '-': '+'); Printf ("\ n % d % c % d =", a, sign1, B); scanf_s ("% d ", & c); if (sign = 1) & (a-B = c) | (sign! = 1) & (a + B = c) printf ("OK! \ N "); else printf (" Incorrect! \ N "); _ getch (); system (" Pause "); return 0 ;}

 

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.