C language bit operation, preprocessing, file operation of C++

bitwise operator

meaning

&

Bitwise AND: When the corresponding two binary bits are both 1, the result is 1, otherwise it is 0

|

Bitwise OR: When one of the corresponding two binary bits is 1, the result is 1, otherwise it is 0

~

Inversion: ~ is a unary operator, turning 0 into 1 and turning 1 into 0

^

Bitwise XOR: When the two corresponding binary digits are different, the result is 1, otherwise it is 0

<<

Left shift: shift the binary bit to the left by the specified number of bits, discard the high bit, and fill the low bit with 0

>>

Right shift: Shift the binary bit to the right by the specified number of digits. When it is a positive number, the high bit is filled with 0, and when the number is negative, the high bit is filled with 0 or 1, depending on the regulations of the compilation system.

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>  





int main()

{

int a = 25;

int b = 29;

int sum = a & b; //按位与

printf("%u \n", sum);



 sum = a | b; //按位或

printf("%u \n", sum);



sum = ~b; //按位取反

printf("%u \n", sum);



sum = a^b; //按位异或

printf("%u \n", sum);



sum = b<<2; //左移动两位

printf("%u \n", sum);



sum = b>>2; //右移动两位

printf("%u \n", sum);



return 0;

}

Circular shift: Put the removed low bit into the high bit of the number, or put the removed high bit into the low bit of the number.

The process of circular left shift is divided into three steps:

  1. Put the left end n bits of x into the lower n bits of z first, z=x>>(32-n);
  2. Shift x to the left by n bits, fill the lower n bits on the right with 0, y=x<<n;
  3. Perform bitwise OR operation on y and z, y=y|z;

The process of circular right shift is divided into three steps:

  1. Put the right n bits of x into the high n bits of z first, z=x<<(32-n);
  2. Shift x to the right by n bits, and add 0 to the high n bits at the left end, y=x>>n;
  3. Perform bitwise OR operation on y and z, y=y|z;

bit segment

A bit segment is a special structure type, the length of all its members is defined in binary units, and the members in the structure are called bit segments. The definition form of bit segment:

Struct structure name{

Type variable name 1: length

.......

}

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>  





int main()

{



struct MyStruct

{

unsigned a : 1; //表示a只占用1个二进制位

unsigned b : 2; //表示b只占用2个二进制位



unsigned  : 0; //表示无名字段,代表后面的内容从这里开始存储

unsigned c : 1; //表示a只占用1个二进制位

unsigned d : 2; //表示b只占用2个二进制位

};



/*

位段类型和位段变量的定义、位段成员的引用,均与结构体类型和结构体变量相同。

位段后面的长度代表该位段占用的二进制位数

*/



//可以使各段占满一个字节,也可以不占满(一个字节8位,也就是结构体内部的各位段长度加起来的和小于8的情况)

struct MyStruct2

{

unsigned a : 1; //表示a只占用1个二进制位

unsigned b : 2; //表示b只占用2个二进制位



unsigned : 0; //表示无名字段,代表后面的内容从这里开始存储

unsigned c : 1; //表示a只占用1个二进制位

unsigned d : 2; //表示b只占用2个二进制位

};



/*

1,一个位段必须存储在一个存储单元中(通常为一个字节),不能跨两个存储单元。如果本单元不够容纳某位段,则从下一个单元开始存储该位段。

2,可以用 %d, %x, %u, %o 等格式字符,以整数形式输出位段。

3,在数值表达式中引用位段时,系统自动将位段转换为整型数。

*/





return 0;

}





preprocessing

Macro definition: It is a kind of preprocessing command, which provides a mechanism that can replace the string in the source code.

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>   



//不带参数的宏定义,宏定义不是C语句,不需要在末尾加上分号。宏定义的好处是:程序中引用该宏的地方在修改时只需要修改一处即可。

#define PI 3.14  

//带参数的宏定义: 不但要替换字符串,还要替换参数

#define tax(a,b) (a+b)  





int main()

{

printf("%d ,",PI);  //把PI替换为3.14

printf("%d",tax(4,6)); //把ax(4,6) ,替换为(4+6)

return 0;

}







#include命令:用于将其他源文件的内容包含进来,也就是将其他文件包含到本文件中。



#include<stdio.h>   

#include "stdio.h"

As shown above, the difference between using the <> sign and using "" is:

1. Use <> as the standard method, and the system will directly search for the files to be included in the directory where the C library function header files are stored.

2. Use "" to include. First, it will search for the file to be included in the user's current directory. If it cannot find it, it will search for the header file to be included in the directory where the C library function header file is stored.

The included files that are often used in the header of the header file are called header files or header files, generally with the suffix .h. The content generally placed in the header file includes: macro definition, structure, union, enumeration declaration, typedef declaration, external function declaration, and global variable declaration.

There are a few things to note about header file inclusion:

  1. A #include command can only specify one included file;
  2. File inclusion can be nested, that is, an included file can also include another included file.
  3. If the header file ah is included in ac, the two will become one file after precompilation. If there is a global static variable in ah, the global variable is also valid in the ac file, and there is no need to declare it with extern.

conditional compilation

By default, all lines in the source program will be compiled. If you want some of the content to be compiled only when certain conditions are met, you need to use the conditional compilation command.

The use of conditional compilation can handle the official version program and the debug version program well, and it will also enhance the portability of the program.

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>   

#define NUM 55



//#if语句:如果if语句后面的表达式为真,则编译 #if 与 #endif 之间的程序段。

#if NUM>50

//这里书写代码

#endif 



//#if #else  语句: 使用类似于常规代码的 if else

#if  NUM==50

//满足条件执行这里

#else

//不满足条件执行这里

#endif





//if  #elif语句 : 多条件语句,类似于if  else if

#if NUM<55

//条件1满足执行这里

#elif  NUM>52

//条件2满足执行这里

#endif





//#ifdef 语句:如果后面跟着的宏已经被定义,则编译后面的程序,如果后面跟着的宏未被定义,则不编译后面的程序,

#ifdef NUM

//如果宏NUM已经被定义,则不编译这里面的程序

#endif 





//#ifdef #else 语句:如果后面跟着的宏已经被定义,则编译后面的程序,如果后面跟着的宏未被定义,则编译#else后面的程序,

#ifdef NUM

//如果宏NUM已经被定义,则不编译这里面的程序

#else

//如果宏NUM已经被定义,则编译这里面的程序

#endif 





//#ifndef 语句:如果后面跟着的宏未被定义,则编译后面的程序,如果后面跟着的宏已经定义,则不编译后面的程序,

#ifndef NUM

//如果宏NUM未被定义,则编译这里面的程序

#endif 





//#ifndef #else 语句:如果后面跟着的宏未被定义,则编译后面的程序,如果后面跟着的宏已经定义,则编译#else后面的程序,

#ifndef NUM

//如果宏NUM未被定义,则编译这里面的程序

#else

//如果宏NUM已经已经定义,则编译这里面的程序

#endif 





// #undef命令 : 删除事先定义好的宏定义,相当于是限制了宏PI的使用范围,即只在#define 与 #undef之间。

#define PI 3.14

//PI只在这里面有效

#undef PI







int main()

{

// #line命令: 用于显示_LINE_ 与 _FILE_ 的内容。 _LINE_用于显示当前编译行的行号  , _FILE_用于存放当前编译的文件名。常用于调试环境中。

printf("当前行号: %d \n", __LINE__);

printf("当前文件名称: %s \n", __FILE__);







//#pragma 命令 : 用于设定编译器状态或指示编译器完成一些特定的动作。格式为://#pragma 参数,参数主要有以下三个:

#pragma message  //在编译信息窗口中输出的相应信息

#pragma code_seg //设置程序中函数代码存放的代码段

#pragma once     //保证头文件被编译一次



/*

标准的预定义宏名有以下几个:

_LINE_  :当前被编译代码的行号

_FILE_  :当前源程序的文件名称

_DATE_  :当前源程序的创建日期

_TIME_  :当前源程序的创建时间

_STDC_  :判断当前编译器是否为标准C,若是标准C则其值为1,否则不是标准C编译器

*/

return 0;

}

document

A file is an ordered collection of related data with a name called a filename.

In C language, the type of operation file is structure FILE, and the way of operation file is FILE *.

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>   

int main()

{

FILE* fp = fopen("a.txt", "r"); //使用指针的方式打开一个文件,且是只读模式

return 0;

}

file usage

meaning

r

Operate text files read-only

w

Operate text files write-only

a

append at the end of the text file

rb

Operate binary files read-only

wb

Operates on binary files write-only

ab

Append at the end of the binary file

r+

Manipulate text file read and write

w+

Manipulate text file read and write

a+

Operate text files to read and write, or append at the end of the file

rb+

Read and write binary files

wb+

Read and write binary files

ab+

Operate binary files to read and write, or append at the end of the file

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>   



int main()

{

//使用指针的方式打开一个文件,且是只读模式,若打开文件失败则返回NULL

FILE* fp = fopen("G:\VS\CDemo\CDemo\c.txt", "a");





char C ='A';

//fputc函数用于向文件中写入一个字符串

fputc(C, fp);



int i = 12;

//fprintf将变量i格式写入到文件中去

fprintf(fp, "%d", i);



//读取fp执行的文件中的i的值

fscanf(fp, "%d", &i);



char a[100];//数据存储块

//fread代表以某种规则来读取数据块

fread(a, 20, 5, fp);  //参数a代表存储数据的块,20代表每次读取的数量,5代表读取的次数,fp是文件指针



//fwrite代表以某种规则写入文件,参数a代表存储数据的块,20代表每次要写的数量,5代表写的次数,fp是文件指针

fwrite(a, 20, 5, fp);



//移动文件内部的位置指针,fp代表要移动的指针,200代表偏移量,0代表起始位置

fseek(fp, 200, 0);



//将位置指针重返文件开头

rewind(fp);



//得到流式文件中的当前位置

ftell(fp);



//文件使用完毕后,关闭文件,关闭成功返回0,否则返回EOF

fclose(fp);



return 0;

}

Guess you like

Origin blog.csdn.net/XiaoWang_csdn/article/details/131019352