gogoWebsite

Enumeration Bit Operation Function Pointer

Updated to 7 hours ago

Table of contents

enumerate

typedef definition

Bit operation

&  and operations

|  or operation

~ Reverse

^ XOR

>>  Write right:

Function pointer

Space on the heap


enumerate

Meaning: List the values ​​of variables one by one; the values ​​of variables are limited to the range of listed values;

Its function:

1. To improve the readability of the code

2. Improve the security of the code

Enumeration type (belongs to the basic type)

grammar

enum enum

{

List various values,

};

Note: When listing various values, just use commas to separate them, and no type names are required.

Give an example

enum fire
{
   litte_fire,
   middle_fier,
   large_fire,
}

This code indicates that an enum type is defined;

Notice:

1. The values ​​listed one by one in the enumeration start from 0 by default; if there is a given value, then the enumeration members that do not have the value are added 1 in turn;

2. The essence of an enum is actually an int type data;

3. Enumerated type variables are common to integer type variables.

4. Comparison and difference with macro definition:

(1) Different usage stages

Macro definitions are used in the preprocessing phase;

Enumerations are required to be checked in the compilation stage and participate in the running of code during the running stage;

(2) Readability

All improve readability; but enumerations can better illustrate the relationship between some values ​​with correlation;

typedef definition

typedef——type define

Its original intentionNot a defined type, it's forType naming.

For example

int a; //Define an int type variable

typedef int a; //A is an alias for type int at this time

Bit operation

It is the feature of C language: it can directly operate binary

&  and operations

Operation rules: If one is false, it will be false (used to clear zero)

1001 1110 
0011 0010 &
-------------------

0001 0010 

|  or operation

Operation rules: One truth is true (often used to set 1)

1001 1110 
0011 0010 |
-------------------

1011 1110 

~ Reverse

Operation rules: true or false

1001 1110  ~
-------------------

0110 0001

^ XOR

Operation rules: the same is 0, different is 1

1001 1110 
0011 0010 &
-------------------

0101 0011 

Note: Floating point numbers cannot perform bit operations
 

<<  Transfer to the left

Writing method: a<<n  (represents that the data a is shifted left by n bits)

0000 0001 
0000 0010
Move left by 1 bit  equivalent to multiply 2

After displacement, make up 0 later;.

>>  Write right:

a>>n (represents the data a to right n bits)

1000

1100

-----------

0100

0010

Move right 1 bit equivalent to divide 2

The right shift of arithmetic is to look at the sign bits and the data type:
1. If it is signed data, when moving right, the highest bit complements the sign bit;

2. If it is unsigned data, the highest bit is filled with 0 when shifted right.

Function pointer

(Pointer to function type; base type is pointer to function type)

A pointer function is a function whose return value is pointer type.

Space on the heap

 void *malloc(size_t size); 

Function:
Create a piece of space on the pile;

Parameter size:

size indicates the size of the applied space, unit bytes;

Return value:

Successfully returned to the address applied to the memory space;
Return NULL if failed.
Release function:

void free(void *ptr);

Function:

Free up space on the pile you applied for previously;
parameter:
ptr must be the address that was applied to the heap space before;
Note 1:
Free space after free
1. It is generally no longer used;
2. The pointer to this space --- is a wild pointer;
After that, do not use the content in the corresponding space;

Note 2:
1. Two functions appear in pairs;
2. Free just frees the space—which means that this space is free again, but the data on this space will not be cleared;
3. Don’t free it many times;