Summary of basic concept knowledge points of C + + programming

Source: Internet
Author: User
Tags bitwise

Summary

Knowledge points include: two assignments of global variables, 1 of the number after conversion to binary, and a non-cyclic expression to determine whether a number is a 2 N-squared, a logical operation and a bitwise-logical operation; the execution order of a printf input expression; A bitwise operation that determines whether the input parameter is a power of 2 The two numeric values of the bitwise operation are averaged and summed; there is a large presence in the variable with no judgment statement; There is no intermediate exchange variable; c + + relationship.

Body

1. Assignment statements

Test centers: Int i=i//global variable;

Test centers: With operations and bitwise and operations;

Summary: = = && | | is a logical operator and the return value is a bool value

#include <iostream>using namespace Std;int i = 1;int main () {cout<<i<<endl;//i = 1int i = i; cout<& lt;i<<endl; i = -858993460int y (4), Z (3); int x = (Y & z); First, y and Z are evaluated and then assigned to xcout<<x<<endl;. x = 0x = (y && z); The Y and Z are first followed by the operation, and then a Boolean value is returned, assigned to xcout<<x<<endl; x = 1return 0;}


Test Center: Calculates the number of 1 in the number after conversion to binary.

int fun (int x) {int con = 0;while (x) {x = x& (x-1); con++;} return con;}


2. i++ Issues

Test Center: The printf function executes the output data from right to left;

Test Center: (++PTR) The next statement of the statement has PTR = ptr+1;

#include <stdio.h>void main () {    int b = 3;    int arr[] = {6,7,8,9,10};    Sint *ptr = arr;    * (ptr++) + = 123;    printf ("%d\n", * (ptr-1)); Output 129    printf ("%d\n", *ptr);//Output 7    printf ("%d,%d\n", *ptr, * (++PTR));//Output 8,8}


3, type of conversion

Test Center: The function is because the floating-point number in memory and integer storage, which is equivalent to the floating-point address of the beginning of the sizeof (int) bytes as an int data output, only when a = 0, int A will be the same as int (&a).

float A;

Int (&a);

Test Center:unsigned char and char bit length;

Test Center: pointer addressing;

#include <stdio.h>int main () {    unsigned int a = 0XFFFFFFF7;    unsigned char i = (unsigned char) A;    Char *b = (char*) &a;    unsigned char *c = (unsigned char*) &a;    printf ("%08x\n%08x\n%08x\n", I, *b, *c);    int *p = 0; // ???    return 0;}


Output results

000000f7
Fffffff7
000000f7
Press any key to continue

Summary: The former is assigned to the unsigned int variable to the unsigned char variable, the byte truncation occurs, the 3-bit and above 3-bit will be automatically discarded by the program, the latter is to convert the unsigned int pointer to a char type pointer, This affects the addressing of the pointer.


4. Problems with operators

Test Center: The priority of the operator;

Test centers: implicit type conversions;

#include <iostream>using namespace Std;int main () {    unsigned char a = 0XA5;    unsigned char b = ~a>>4+1;    printf ("b=%d\n", b);    return 0;}


The run result is 250.
Summary: C + + operator precedence formula! ~ + +--& +->> << = = = ^ | && | | ?:
First, the values of a and 4 are converted to int, which is an integer promotion, followed by subsequent operations. You can know that the + priority is higher than >>, so, first calculate 4+1 so you can know first to take a reverse after the right shift 5 bits. When the result is obtained, the value is converted to unsigned char and then to B.

There is also a particularly error-prone place where we cannot convert 0xa5 to 1010 0101 We should write 16, and then because unsigned char can only represent a low 8-bit value, it is 250.

Test Center: Determine if a number is 2 N power

! (X & (X-1))

Using a non-cyclic expression to determine whether a number is a 2 N-square, 2^n is the binary 10,100,1000 ...

Therefore, the expression is 0, then judged to be 2 of the power of N.

Test Center : Use an expression to take two-digit averages
Return ((x & y) + (x ^ y) >> 1)
X&y take the same bit with, equal to the same position reserved and binary, (x^y) >> 1 take different bits of x and Y, move right equivalent to binary, then take average.
Test Center: The addition of two integers using bit operations
int Add (int a,int b) {    if (b = = 0)        return a;//no rounding    int sum, carry;    sum = a ^ b; Completion of addition operations without carry    carry = (A & B) << 1;//completion is and left shift operation    return ADD (sum. carry);//Recursive addition}


5, A, b exchange and comparison
Test Center : Do not use judgment and select the statement, find out two numbers in the larger.

int max = ((a+b) + ABS (A-B))/2; Scenario 1
Test Center: No intermediate variables, exchange A, B.
A = a ^ b;//with XOR or statement
b = a ^ b;
A = a ^ b;

6, C and C + + relationships
1) in a C + + program called by the C compiler compiled function, why add extern "C"?
The C + + language supports function overloading, and the C language does not support it, assuming that the prototype of a function is void foo (int x,int y) The function is compiled by the C compiler in the library with the name _foo, and the C + + compiler produces names like _foo_int_int, function to solve the problem of name matching.

2) C language is a structured language, focusing on algorithms and data structures, the design of C program first of all to consider is how to use a process of input processing to get output, and for C + + first consideration is how to construct an object model, so that the model can fit with the problem domain, The control of the output or implementation process is obtained by obtaining the state information of the object. use an expression to determine whether a number is 2 of the n-th square, and cannot use a cyclic tone

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

Summary of basic concept knowledge points of C + + programming

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.