1. Bit operation

* Place 1    use |
* clear 0    use &
* Negation     use ^ #include <stdio.h> int main(void) { // The value of a register bit4～bit7 Negation , Other bits remain unchanged
unsigned int a = 0x123d0c37; unsigned int b = 0xf0; unsigned int c; c = a ^ b;
printf("a & b = 0x%x.\n", c);//0x123d0cc7. /* // The value of a register bit4～bit7 Place 1, Other bits remain unchanged
unsigned int a = 0x123d0cd7; unsigned int b = 0xf0; unsigned int c; c = a | b;
printf("a & b = 0x%x.\n", c); */ /* // The value of a register bit13～21 clear 0, Other bits remain unchanged unsigned int a
= 0x123d0c57; unsigned int b = 0xffc01fff; unsigned int c; c = a & b; printf("a
& b = 0x%x.\n", c); // 0x12000c57. */ }
Using macros to modify variables a Of bit n To set , clear 0, Reverse operation

Suppose there is an integer of x, Write three will x The second of the binary bits of n position 1, Clearing , Negation , Other bits remain unchanged .

1. Clearing

if there be x=10, The binary representation is ：

00000000 00000000 00000000 00001010, The rightmost bit of a binary is called the first bit , For example, the second 1 Qingwei 0, Then ：

00000000 00000000 00000000 00001000 = 8,

Place the third position as 1, Then ：   00000000 00000000 00000000 00001110 = 14.

Reverse the first , Then ：   00000000 00000000 00000000 00001011 = 11.
#include <iostream> using namespace std; #define IBS(n) 0x01<<(n-1) void
Set_N_To_1(int &x, int n) { x |= IBS(n); } void Clear_N_To_0(int &x, int n) { x
&= ~IBS(n); } void Negate_N(int &x, int n) { x ^= IBS(n); } int main() { int x
= 10; Set_N_To_1(x, 3); cout<<x<<endl; x = 10; Clear_N_To_0(x, 2);
cout<<x<<endl; x = 10; Negate_N(x, 1); cout<<x<<endl; return 0; } #include
<iostream> using namespace std; #define Clear_N_To_0(x, n) (x & (~(1 <<
(n-1)))) #define Set_N_To_1(x, n) (x | (1 << (n-1))) #define Negate_N(x, n) (x
^ (1 << (n-1))) int main() { cout << Clear_N_To_0(10, 2) << endl; cout <<
Set_N_To_1(10, 3) << endl; cout << Negate_N(10, 1) << endl; return 0; }
Note: in the definition of macro n-1, Just for better understanding , For example, the first position is Set_N_To_1(x, 1), From subscript 1 Start counting

2.strcpy() function Standard implementation

C Language standard library function
Prototype statement ：extern char* strcpy(char *dst,const char *src);
Header file ：string.h
function ： hold src Reference by NULL Copy the ending string to dst In the array .
explain ：src and dst The indicated memory area cannot overlap and dst There must be enough space to accommodate it src String of .
Return to point dst Pointer to .

strcpy Function will str Copy to output parameter dst in , At the same time, the return value of the function is dst. This is not unnecessary , The following flexibility can be obtained ：
char str; int length = strlen( strcpy(str, “Hello World”) );
effect ： To generate chain expressions .
/* C Language standard library function strcpy A typical industrial level minimalist implementation of Return value ： Returns the address of the target string .
For abnormal cases ANSI-C99 The standard is not defined , So the implementer decides the return value , Usually NULL. parameter ： Target string dest Source string str */ char*
strcpy(char* dst, const char* str) { //1. Assertion assert(dst != NULL && str != NULL);
//2. use ret point dst character string char* ret = dst; //3. copy while(*str != '\0') { *dst = *str;
src++; dst++; } *dst = '\0'; return ret; }
be careful ：

* input parameter const
* Add negation to source address and destination address 0 Assertion
* In order to realize chain operation , Return destination address to
3.strcat() function Standard implementation
// hold src The string pointed to is appended to the dst The end of the string pointed to . char* strcat(char* dst, const char* str) {
//1. Use pointer to point to dst character string char* ret = dst; //2. Move pointer to dst The end of the string while(*ret) ret++; //3. Splicing
while(*src != '\0') { *ret++ = *str++; } *ret = '\0'; return dst; }
4.strcmp() function Standard implementation

hold str1 The string and str2 The string that you point to .

int strcmp(const char *str1, const char *str2)
parameter
str1 -- The first string to compare .
str2 -- The second string to compare .
Return value
The return value of this function is as follows ：

If the return value < 0, It means str1 less than str2.
If the return value > 0, It means str2 less than str1.
If the return value = 0, It means str1 be equal to str2.
// hold str1 The string and str2 The string that you point to . int strcmp(const char *str1, const char
*str2) { int ret = 0; while( !(ret = *(unsigned char*)str1 - *(unsigned
char*)str2) && *str1) { str1++; str2++; } if( ret < 0 ) { return -1; } else if(
ret > 0 ) { return 1; } return 0; }
5.strstr() function Standard implementation

char* strstr(const char *s1, const char *s2)

Return value ： if s2 yes s1 Substring of , Then return s2 stay s1 First address of ; If s2 no s1 Substring of , Then return NULL.
char* strstr(const char* s1, const char* s2) { int len2; if( !(len2 =
strlen(s2)) )// In this case s2 Cannot point to null , otherwise strlen The length cannot be measured , This statement is wrong { return (char*)s1; } for( ;
*s1; ++s1) { if( *s1 == *s2 && strncmp(s1, s2, len2) == 0) return (char*)s1; }
return NULL; }
6.TCP How to guarantee the reliability of protocol ?

(1)TCP Before transmitting valid information, both sides of communication must shake hands , Establish a connection to communicate
(2)TCP After receiving the packet, the recipient of ack To sender , If not received by sender ack Packet loss and retransmission
(3)TCP Validation is attached to the valid data content of , To prevent content from being corrupted during delivery
(4)TCP The adaptation rate will be adjusted automatically according to the network bandwidth （ Sliding window technology ）
(5) The sender will number each split message , The receiver will verify the number , Once the sequence is wrong, it will be retransmitted .

Technology
Daily Recommendation
views 4