位运算:
Part1:
#include <iostream> using namespace std; int main(int argc, char *argv[]) { //unsigned char per byte that contain 00000000 - 11111111 unsigned char a=2; // bin->0000 0010 unsigned char b = ~a; //~(0000 0010) result:(1111 1101) or 253 printf("%d ",b); unsigned char c=3; //0000 0011 unsigned char d=2; //0000 0010 printf(" & %d ",c&d); // c&d -> 0000 0010 : 2 printf(" | %d ",c|d); // c|d -> 0000 0011 : 3 //十六进制->十进制 hexadecimal(hex)->base10 int d_hex = 0xA3F; //A-..-F (10-15) //0xA3F -> A3F -> 10*16^2 + 3*16^1 + 15*16^0 = 2623 printf("0xA3F : %d ",d_hex); // 2623 int d_hex_zero = 0x000; //0*16^2 + 0*16^1 + 0*16^0 = 0 printf("0x000 : %d ",d_hex_zero); int d_hex_1 = 0x001; //0*16^2 + 0*16^1 + 1*16^0 = 1 printf("0x001 : %d ",d_hex_1); int d_hex_2 = 0x011; //0*16^2 + 1*16^1 + 1*16^0 = 17 printf("0x001 : %d ",d_hex_2); return 1; }
Part2:
#include <iostream> using namespace std; int main() { // int is 32 bit int a = 10; //00000000 00000000 00000000 00001010 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 10 int b = 5; //00000000 00000000 00000000 00000101 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 = 5 int c = a | b ; // 00000000 00000000 00000000 00001111 1+2+4+8 = 15 cout << c <<endl; int d = c|0xFF; //hex: 0xFF = 15*16^1 + 15*16^0 = 255 bin:255-> 1111 1111 cout << d <<endl; // ^ int e = a ^ b; //00000000 00000000 00000000 00001111 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 15 cout << e <<endl; // swap a b // a 00000000 00000000 00000000 00001010 // b 00000000 00000000 00000000 00000101 a = a ^ b ; // a->00000000 00000000 00000000 00001111 b = b ^ a ; // b->00000000 00000000 00000000 00001010 a = a ^ b ; // 00000000 00000000 00000000 00000101 cout << "after swap a,b a:" << a << " " << "b:"<<b <<endl; return 0; }
C语言的几个输入输出函数
#include <stdio.h>
getchar(),putchar()
scanf(),printf()
1->getchar()与scanf()唯一的区别是getchar()不会跳过' '或者空格,例如如下
#include <stdio.h> #include <stdlib.h> int main() { int ch; int a,b; printf("enter a char ,enter to quit "); while((ch=getchar()) != ' ' ) { if (scanf("%d %d",&a,&b)!=2) break; printf("you enter the %c %d %d ",ch,a,b); // getchar();//the ch a b enter complete will leave a ' ' while(getchar() != ' ') // GNU BOOK always use this { continue; } printf("PLEASE ENTER ANOTHER CHAR "); } exit(0); }
如果没有while循环中的while(getchar() != ' ') ,那么程序输入一轮直接结束,原因是scanf()会把回车 放到输入队列。所以要剔除输入的回车
2->如何确定输入类型
scanf()返回值是其成功读入的项目个数。比如scanf("%d %d",&a,&b)==2
如下面代码,如果输入q,则state=0
int main() { int input; int state = scanf("%d",&input); printf("state = %d,and input value is %d,",state,input); exit(0); }
输入输出大集合:
char temp[81]; printf("Please enter your first name "); // STDIN //scanf("%s",temp); // jump backspace //fscanf(stdin,"%s",temp); // jump backspace fgets(temp,81,stdin); // do not jump backspace //gets(temp); // do not jump backspace // STDOUT //printf("You enter the strings -> : %s ",temp); //fprintf(stdout,"You enter the strings -> : %s " ,temp); //fputs(temp,stdout); puts(temp); // CHAR putchar() getchar() //getc(FILE*) putc(char,FILE*) FILE *in; FILE *out; in = fopen("test.txt","r"); if(in==NULL){return;} out = fopen("test.txt","w"); if(out==NULL){ return;} int ch; while((ch=getc(in))!=EOF) // COPY DATA PER CHAR { putc(ch,out); }
宏定义:
#include <stdio.h> #define PSQR(x) printf("Value squre of "#x" is %d ",(x*x)); #define XNAME(n) x##n #define SIZE "HOUDINI" #ifdef SIZE // if define the SIZE #define SIZE "MAYA" #endif #ifdef __GNUC__ //if define the __GUNC__ #define HOUDINI 2 #else #define HOUDINI 3 #endif #ifndef __SIZEOFINT__ // becuase __SIZEOFINT__ do not define,that "if not define the __SIZEOFINT__" #define __SIZEOFINT__ sizeof(int) // so can arrive this code #endif #define NOFLOAT 1 #if NOFLOAT == 1 //check NOFLOAT equal 1,then #include<float.h> #include <float.h> #elif NOFLOAT == 2 #include <typeinfo.h> #elif NOFLOAT == 3 #include <typeinfo.h> #else #include <stdarg.h> #endif #if defined(NOFLOAT) // if define NOFLOAT ------ same as #ifdef NOFLOAT #define A 2 #endif int main(int argc, char *argv[]) { printf("Hello World! "); PSQR(2*4); char name[20]; fprintf(stdout,"enter the name "); fgets(name,20,stdin); int i=0; return 0; }
定义一个简单的宏:
#define GARRSERT(EXP) { if(EXP != 1) { printf("Assert expression "#EXP " ERROR %s IN %d line ",__FILE__,__LINE__); exit(0); } }
GARRSERT(3>5);
strycpy strlen
#include <stdio.h> #include <stddef.h> void gstrcpy(char *dst,char const *str); void gstrcpy2(char *dst, char const *str); size_t gstrlen(char *str); int main() { char *src = "houdini"; char dst[8]={'