E x c e r c i s e 2 − 1 Excercise\quad 2-1 Excercise2−1:输出结果如图1和图2所示,这道练习题需要文章1和文章2的知识。
#include <stdio.h>
#include <limits.h>float getFloat(char sign, unsigned char exp, unsigned mantissa);
double getDouble(char sign, unsigned short exp, unsigned long long mantissa);int main(void)
{printf("sizeof(char)=%d\n",sizeof(char));printf("Signed char[%d to %d]\n", (unsigned char)(~(((unsigned char)~0) >> 1)), (unsigned char)(((unsigned char)~0 >> 1)));printf("Unsigned char[0 to %u]\n", ((unsigned char)~0)); printf("sizeof(short)=%d\n",sizeof(short));printf("Signed short[%d to %d]\n", (unsigned short)(~(((unsigned short)~0) >> 1)), (unsigned short)(((unsigned short)~0 >> 1)));printf("Unsigned short[0 to %u]\n", (unsigned short)((unsigned short)~0));printf("sizeof(int)=%d\n",sizeof(int));printf("Signed int[%d to %d]\n", (unsigned int)(~(((unsigned int)~0) >> 1)), (unsigned int)(((unsigned int)~0 >> 1)));printf("Unsigned int[0 to %u]\n", (unsigned int)((unsigned int)~0));printf("sizeof(long)=%d\n",sizeof(long));printf("Signed long[%ld to %ld]\n",(unsigned long)(~(((unsigned long)~0) >> 1)), (unsigned long)(((unsigned long)~0 >> 1)));printf("Unsigned long[0 to %lu]\n",(unsigned long)((unsigned long)~0));printf("sizeof(long long)=%d\n",sizeof(long long));printf("Signed long long[%lld to %lld]\n", (unsigned long long)(~(((unsigned long long)~0) >> 1)), (unsigned long long)(((unsigned long long)~0 >> 1)));printf("Unsigned long long[0 to %llu]\n", (unsigned long long)((unsigned long long)~0));printf("---------------------------------------------------------------\n");printf("Signed char[%d to %d]\n", SCHAR_MIN, SCHAR_MAX);printf("Unsigned char[0 to %u]\n", UCHAR_MAX); printf("---------------------------------------------------------------\n");printf("Signed short[%d to %d]\n", SHRT_MIN, SHRT_MAX);printf("Unsigned short[0 to %u]\n", USHRT_MAX);printf("---------------------------------------------------------------\n");printf("Signed int[%d to %d]\n", INT_MIN, INT_MAX);printf("Unsigned int[0 to %u]\n", UINT_MAX);printf("---------------------------------------------------------------\n");printf("Signed long[%ld to %ld]\n",LONG_MIN, LONG_MAX);printf("Unsigned long[0 to %lu]\n",ULONG_MAX);printf("---------------------------------------------------------------\n");printf("Signed long long[%lld to %lld]\n", LLONG_MIN, LLONG_MAX);printf("Unsigned long long[0 to %llu]\n", ULLONG_MAX);printf("---------------------------------------------------------------\n");printf("Positive Normalized Float[%g to %g]\n", getFloat(0, 0x01, 0x00000000), getFloat(0, 0xFE, 0x007FFFFF));printf("Negative Normalized Float[%g to %g]\n", getFloat(1, 0xFE, 0x007FFFFF), getFloat(1, 0x01, 0x00000000));printf("Positive De-Normalized Float[%g to %g]\n", getFloat(0, 0x00, 0x00000001), getFloat(0, 0x00, 0x007FFFFF));printf("Negative De-Normalized Float[%g to %g]\n", getFloat(1, 0x00, 0x007FFFFF), getFloat(1, 0x00, 0x00000001));printf("Positive De-Normalized Zero Float[%g]\n", getFloat(0, 0x00, 0x00000000));printf("Negative De-Normalized Zero Float[%g]\n", getFloat(1, 0x00, 0x00000000));printf("Positive Infinity Float[%g]\n", getFloat(0, 0xFF, 0x00000000));printf("Negative Infinity Float[%g]\n", getFloat(1, 0xFF, 0x00000000)); printf("Not A Number Float[%g]\n", getFloat(0, 0xFF, 0x00000010)); printf("Not A Number Float[%g]\n", getFloat(1, 0xFF, 0x00000001)); printf("Positive Normalized Double[%g to %g]\n", getDouble(0, 0x0001, 0x0000000000000000), getDouble(0, 0x07FE, 0x000FFFFFFFFFFFFF));printf("Negative Normalized Double[%g to %g]\n", getDouble(1, 0x07FE, 0x000FFFFFFFFFFFFF), getDouble(1, 0x0001, 0x0000000000000000));printf("Positive De-Normalized Double[%g to %g]\n", getDouble(0, 0x0000, 0x0000000000000001), getDouble(0, 0x0000, 0x000FFFFFFFFFFFFF));printf("Negative De-Normalized Double[%g to %g]\n", getDouble(1, 0x0000, 0x000FFFFFFFFFFFFF), getDouble(1, 0x0000, 0x0000000000000001));printf("Positive De-Normalized Zero Double[%g]\n", getDouble(0, 0x0000, 0x0000000000000000));printf("Negative De-Normalized Zero Double[%g]\n", getDouble(1, 0x0000, 0x0000000000000000));printf("Positive Infinity Double[%g]\n", getDouble(0, 0x07FF, 0x0000000000000000));printf("Negative Infinity Double[%g]\n", getDouble(1, 0x07FF, 0x0000000000000000)); printf("Not A Number Double[%g]\n", getDouble(0, 0x07FF, 0x0000000000000010)); printf("Not A Number Double[%g]\n", getDouble(1, 0x07FF, 0x0000000000000001)); return 0;
}float getFloat(char sign, unsigned char exp, unsigned mantissa)
{unsigned f = ((unsigned)(((unsigned)(sign != 0))) << 31) | ((unsigned)(((unsigned)exp) << 23)) | ((unsigned)(mantissa & 0x007FFFFF));return *((float *)&f);
}double getDouble(char sign, unsigned short exp, unsigned long long mantissa)
{unsigned long long d = ((unsigned long long)(((unsigned long long)(sign != 0)) << 63)) | ((unsigned long long)((unsigned long long)(exp & 0x07FF) << 52)) | ((unsigned long long)(mantissa & 0x000FFFFFFFFFFFFF));return *((double *)&d);
}
E x c e r c i s e 2 − 2 Excercise\quad 2-2 Excercise2−2:
#include <stdio.h>#define MAXLINE (1000)int getline(char s[], int lim);int main()
{int len; /* current line length */char line[MAXLINE]; /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("%s",line);}return 0;
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar(); if((c == '\n')){break;}else if((c== EOF)){break;} else{s[i] = c; }}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}
E x c e r c i s e 2 − 3 Excercise\quad 2-3 Excercise2−3:
#include <stdio.h>#define MAXLINE (1000)int getline(char s[], int lim);
int htoi(char s[]);int main()
{int result=0;int len; /* current line length */char line[MAXLINE]; /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("%s\n",line);result=htoi(line);printf("result=%d\n",result); }return 0;
}int power(int ex)
{int i=0;int result=1; while(i<ex){result=result*16; i=i+1; }return result;
}int htoi(char s[])
{int i=0; int result=0; int prefix=0; int s_index=0;int e_index=0;while(s[i]!='\0'){if(((s[i]>='0')&&(s[i]<='9'))||((s[i]>='a')&&(s[i]<='f'))||((s[i]>='A')&&(s[i]<='F'))){}else{if(i==1){if(s[0]=='0'){if((s[1]=='x')||(s[1]=='X')){prefix=1;}else{printf("Hexadecimal string error\n");return -1; }}else{printf("Hexadecimal string error\n");return -1; }}else{printf("Hexadecimal string error\n");return -1;} }i=i+1; } s_index=i; if(s_index==0){printf("Hexadecimal string error\n");return -1;}if(s_index==1){if((s[i]>='0')&&(s[i]<='9')){return ((int)(s[i]-'0')); } if((s[i]>='a')&&(s[i]<='f')){return ((int)((s[i]-'a')+10)); }if((s[i]>='A')&&(s[i]<='F')){return ((int)((s[i]-'A')+10)); } } if(s_index==2){ if(prefix==1){printf("Hexadecimal string error\n");return -1; }else{for(i=0;i<s_index;i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result + ((int)(s[s_index-1-i]-'0')) *power(i); } if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result + ((int)((s[s_index-1-i]-'a')+10)) *power(i); }if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result + ((int)((s[s_index-1-i]-'A')+10)) *power(i); } } } }if(s_index>2){ if(prefix==1){for(i=0;i<(s_index-2);i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result + ((int)(s[s_index-1-i]-'0')) *power(i); } if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result + ((int)((s[s_index-1-i]-'a')+10)) *power(i); }if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result + ((int)((s[s_index-1-i]-'A')+10)) *power(i); } } } else{for(i=0;i<s_index;i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result + ((int)(s[s_index-1-i]-'0')) *power(i); } if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result + ((int)((s[s_index-1-i]-'a')+10)) *power(i); }if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result + ((int)((s[s_index-1-i]-'A')+10)) *power(i); } } } }return result;
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar(); if((c == '\n')){break;}else if((c== EOF)){break;} else{s[i] = c; }}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}
E x c e r c i s e 2 − 4 Excercise\quad 2-4 Excercise2−4:
#include <stdio.h>#define MAXLINE (1000)int getline(char s[], int lim);
void squeeze(char s1[],char s2[]);int main()
{int result=0;int len; /* current line length */char line[MAXLINE]; /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("before:%s\n",line);squeeze(line,"aFEb");printf("after:%s\n",line); }return 0;
}int is_str_ch(char s[],char c)
{int i=0;char temp_c=0; int result=0; while((temp_c=s[i++])!='\0'){ if(temp_c==c){result=1;break; } }return result;
}void squeeze(char s1[],char s2[])
{int i,j;for(i=j=0;s1[i]!='\0';i++){ if(!is_str_ch(s2,s1[i])){s1[j++]=s1[i]; } }s1[j]='\0';
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar(); if((c == '\n')){break;}else if((c== EOF)){break;} else{s[i] = c; }}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}
E x c e r c i s e 2 − 5 Excercise\quad 2-5 Excercise2−5:
#include <stdio.h>#define MAXLINE (1000)int getline(char s[], int lim);
int any(char s1[],char s2[]);int main()
{int position=0;int len; /* current line length */char line[MAXLINE]; /* current input line */while ((len = getline(line, MAXLINE)) > 0) {position=any(line,"aFEb");printf("position=%d\n",position); }return 0;
}int any(char s1[],char s2[])
{int i,j;for(i=0;s1[i]!='\0';i++){ for(j=0;s2[j]!='\0';j++){if(s1[i]==s2[j]){return i; } } }return -1;
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar(); if((c == '\n')){break;}else if((c== EOF)){break;} else{s[i] = c; }}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}
E x c e r c i s e 2 − 6 Excercise\quad 2-6 Excercise2−6:
#include <stdio.h>unsigned setbits(unsigned x,int p,int n,unsigned y);int main()
{unsigned x=0xFFFFFFDF;unsigned y=0xFFFFFFF2;unsigned p=0;p=setbits(x,5,2,y);printf("p=%08x\n",p); return 0;
}unsigned setbits(unsigned x,int p,int n,unsigned y)
{unsigned front=((~((unsigned)0))<<(p+1));unsigned back=~((~((unsigned)0))<<(p-n+1));unsigned all=front | back; x=x & all; y=(~((~0)<<n)) & y;y=y<<(p-n+1);x=x | y;return x;
}
E x c e r c i s e 2 − 7 Excercise\quad 2-7 Excercise2−7:
#include <stdio.h>unsigned invert(unsigned x,int p,int n);int main()
{unsigned x=0xFFFF55FF;unsigned inverted=0;inverted=invert(x,15,8);printf("inverted=%08x\n",inverted); return 0;
}unsigned invert(unsigned x,int p,int n)
{int i;unsigned temp1_x=x & (~((~((~((unsigned)0))<<n))<<(p-n+1)));unsigned temp2_x=0; unsigned index=1<<p;for(i=0;i<n;i++){if(!(index & x)){temp2_x|=index; } index=index>>1; }return (temp1_x) | (temp2_x);
}
E x c e r c i s e 2 − 8 Excercise\quad 2-8 Excercise2−8:
#include <stdio.h>unsigned rightrot(unsigned x,int n);int main()
{unsigned x=0xFFFFFF5A;unsigned rotated=0;rotated=rightrot(x,8);printf("rotated=%08x\n",rotated); return 0;
}unsigned rightrot(unsigned x,int n)
{unsigned temp1_x=x & (~((~((unsigned)0))<<n));temp1_x=temp1_x<<((sizeof(unsigned) * 8)-n);unsigned temp2_x=(x>>n) & ((~((unsigned)0)>>n)); return (temp1_x) | (temp2_x);
}
E x c e r c i s e 2 − 9 Excercise\quad 2-9 Excercise2−9:当 x x x为奇数的时候, x x x的二进制表示的最低位肯定为1,那么此时 x − 1 x-1 x−1的值二进制表示和 x x x的二进制表示基本一样,除了二进制表示的最低位为0,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x−1)就会将 x x x的二进制表示的最低位清零,其它位保持不变。假设此时 x = 213 x=213 x=213,那么它的二进制表示为 11010101 11010101 11010101,此时 x − 1 x-1 x−1的值二进制表示为 11010100 11010100 11010100,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x−1)之后 x x x的值为 11010100 11010100 11010100= x − 1 x-1 x−1。当 x x x为偶数的时候, x − 1 x-1 x−1会将 x x x的二进制表示的最右的比特值为1的比特位的值变成0,同时将 x x x的二进制表示的最右的比特值为1的比特位后面所有为0的比特位的值都变成1,因此总的来说操作 x & = ( x − 1 ) x\&=(x-1) x&=(x−1)会将 x x x的二进制表示的最右的比特值为1的比特位清零,其它位保持不变。假设此时 x = 208 x=208 x=208,那么它的二进制表示为 11010000 11010000 11010000,此时 x − 1 x-1 x−1的值二进制表示为 11001111 11001111 11001111,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x−1)之后 x x x的值为 11000000 11000000 11000000。因此总结来说不论 x x x为奇数还是偶数,操作 x & = ( x − 1 ) x\&=(x-1) x&=(x−1)都会将 x x x的二进制表示的最右的比特值为1的比特位清零,因此可以利用这个现象来统计 x x x中比特位的值为1的比特位的个数。
#include <stdio.h>int bitcount(unsigned x);int main()
{unsigned x=0x84211234;unsigned num_of_1_bits=0;num_of_1_bits=bitcount(x);printf("num_of_1_bits=%d\n",num_of_1_bits); return 0;
}/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{int b;for (b = 0; x != 0; x &= (x-1))b++;return b;
}
E x c e r c i s e 2 − 10 Excercise\quad 2-10 Excercise2−10:
#include <stdio.h>char lower(char c);int main()
{char c='A';printf("lower(c)=%c\n",lower(c)); return 0;
}/* lower: convert c to lower case: ASCII only */
char lower(char c)
{return ((c>='A' && c<='Z') ? (c-'A'+'a'):c);
}