Chinaunix首页 | 论坛 | 博客
  • 博客访问: 475433
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 13:30:12


点击(此处)折叠或打开

  1. 1.#对计算机不存在有无符号数,只是对单元数据的解释不同,存储的数据不变
  2. #include <stdio.h>

  3. int main(int argc, const char *argv[])
  4. {
  5.     int n=-1;
  6.     printf("%d %u 0x%x\n",n,n,n);
  7.     return 0;
  8. }
  9. 2.#当前的编译器把char类型解释为有符号数,char的范围是-128~127
  10.   #打印else后的语句
  11. #include <stdio.h>

  12. int main(int argc, const char *argv[])
  13. {
  14.     char c=128;
  15.     //char=int 将int截断为char型
  16.     if(c==128)
  17.     //将c进行int型提升,进行符号位扩展,前24位为1,底8位为10000000
  18.         printf("c==128\n");
  19.     else
  20.         printf("c!=128\n");
  21.     return 0;
  22. }
  23. 3.#类型的扩展
  24.   #打印else后的语句
  25. #include <stdio.h>

  26. int main(int argc, const char *argv[])
  27. {
  28.     unsigned int n=6;
  29.     int m=-10;
  30.     if((m+n)<n)
  31.     //m扩展为unsigned型,进行符号位扩展,前24位为1,后8位为256-10原码的形式
  32.         printf("m+n);
  33.     else
  34.         printf("m+n>n=n\n");
  35.     return 0;
  36. }
  37. 4.#验证-2147483648,int型无法存储在内存内
  38. #include <stdio.h>

  39. int main(int argc, const char *argv[])
  40. {
  41.     printf("%d\n",-2147483648); //有警告,内存不存储该数
  42.     printf("%d\n",-2147483647-1); //无警告,进行算数运算,均在int型范围之内
  43.     return 0;
  44. }
  45. 5.#验证是否越界和是否进行数据类型转换,打印a>b
  46. #include <stdio.h>

  47. int main(int argc, const char *argv[])
  48. {
  49.     int a=-2147483647;
  50.     int b=-2147483647-1;
  51.     if(a>b)
  52.         printf("a>b\n",a,b);
  53.     if(-a<-b) //b越界,进行整形转换为0
  54.         printf("-a<-b\n");

  55.     a=2147483647;
  56.     b=-1;
  57.     if((a-b)>0) //a-b越界,进行整形转换为0
  58.         printf("(a-b)>0\n");
  59.     return 0;

  60. }
  61. 6.#不能对浮点数进行取反操作,有错误
  62. #include <stdio.h>

  63. int main(int argc, const char *argv[])
  64. {
  65.     float f=3.14;
  66.     ~f;
  67.     return 0;
  68. }
  69. 7.#输入一个数,实现对0~3位清0,在对0~3位取反,和4~7位置1
  70. #include <stdio.h>

  71. int main(int argc, const char *argv[])
  72. {
  73.     unsigned n;
  74.     printf("n=0x");
  75.     scanf("%x",&n);
  76.     printf("(0~3)clean=%#x\n",n&0xfffffff0);
  77.     printf("(4~7)set=%#x\n",n|0xf0);
  78.     printf("(0~3)opp=%#x\n",n^0xf);
  79.     return 0;
  80. }
  81. 8.#s+=5并不总是等价于s=s+5
  82. #include <stdio.h>

  83. int main(int argc, const char *argv[])
  84. {
  85.     int i=0,a[10]={1,2,3,4,5};
  86.     a[i++]+=5; //与a[i++]=a[i++]+5不等价
  87.     printf("%d\n",a[0]);
  88.     return 0;
  89. }
  90. 9.#用位操作判断一个数是奇数还是偶数
  91. #include <stdio.h>

  92. int main(int argc, const char *argv[])
  93. {
  94.     int n;
  95.     printf("n=");
  96.     scanf("%d",&n);
  97.     if(n&1)
  98.         printf("%d is a odd\n",n);
  99.     else
  100.         printf("%d is a even\n",n);
  101.     return 0;
  102. }
  103. 10.#通过位运算将整形数据的某些位清0和某些位置1
  104.    #cleanbits(unsigned int n,int start,int end)
  105.    #setbits(unsigned int n,int start,int end)
  106. #include <stdio.h>

  107. unsigned cleanbits(unsigned n,int start,int end)
  108. {
  109.     return n&(((~0<<(end-start+1))<<start)|~(~0<<start));
  110. }
  111. unsigned setbits(unsigned n,int start,int end)
  112. {
  113.     return n|(~(~0<<(end-start+1))<<start);
  114. }
  115. int main(int argc, const char *argv[])
  116. {
  117.     unsigned n;
  118.     int s,e;
  119.     printf("num0x start end\n");
  120.     scanf("%x %d %d",&n,&s,&e);
  121.     if(s>=e||s<0||e>32)
  122.         printf("error s to e\n");
  123.     printf("clean(s,e)=%#x\n",cleanbits(n,s,e));
  124.     printf("set(s,e)=%#x\n",setbits(n,s,e));
  125.     return 0;
  126. }
  127. 11.#通过位运算获取某个整形术的某些位
  128.    #getbits(unsigned int n,int start,int end)
  129. #include <stdio.h>

  130. unsigned getbits(unsigned n,int start,int end)
  131. {
  132.     return (n>>start)&(~(~0<<end-start+1));
  133. }

  134. int main(int argc, const char *argv[])
  135. {
  136.     unsigned n;
  137.     int start,end;
  138.     printf("num0x start end\n");
  139.     scanf("%x %d %d",&n,&start,&end);
  140.     if((start>=end)|(start<0)|(end>31))
  141.         printf("error start or end\n");
  142.     printf("(%d~%d)getbits(%x)=%#x\n",start,end,n,getbits(n,start,end));
  143.     return 0;
  144. }
  145. 12.#若有unsigned short a=0x1234,b=0x5678,获取a的高字节,b的低字节组成新值,结果为:0x1278
  146. #include <stdio.h>

  147. int main(int argc, const char *argv[])
  148. {
  149.     unsigned short int a,b,c;
  150.     printf("a b(0x):");
  151.     scanf("%hx %hx",&a,&b);
  152.     c=(a&0xff00)|(b&0xff);
  153.     printf("new_num=0x%hx\n",c);
  154.     return 0;
  155. }
  156. 13.#实现unsigned short数据的字节交换
  157. #include <stdio.h>

  158. unsigned short swap(unsigned short n)
  159. {
  160.     return (n>>8)|(n<<8);
  161. }

  162. int main(int argc, const char *argv[])
  163. {
  164.     unsigned short n;
  165.     printf("n=0x:");
  166.     scanf("%hx",&n);
  167.     printf("swap(0x%hx)=0x%hx\n",n,swap(n));
  168.     return 0;
  169. }
  170. 14.#求unsigned int型数据中有多少个1
  171. #include <stdio.h>

  172. int main(int argc, const char *argv[])
  173. {
  174.     unsigned n;
  175.     int i,cnt=0;
  176.     printf("n=0x");
  177.     scanf("%x",&n);
  178.     for (i = 0; i < 32; i++)
  179.     {
  180.         if(n&1)
  181.             cnt++;
  182.         n=n>>1;
  183.     }
  184.     printf("cnt=%d\n",cnt);
  185.     return 0;
  186. }
  187. 15.#实现unsigned int型数据的二进制打印
  188. #include <stdio.h>

  189. int main(int argc, const char *argv[])
  190. {
  191.     unsigned n;
  192.     int i;
  193.     printf("n=0x");
  194.     scanf("%x",&n);
  195.     printf("binary=");
  196.     for (i = 31; i >= 0; i--)
  197.     {
  198.         if((i+1)%8==0)
  199.             printf(" ");
  200.         printf("%u",(n>>i)&1);
  201.     }
  202.     printf("\n");
  203.     return 0;
  204. }
  205. 16.#通过位操作实现字母的大小写转化
  206. #include <stdio.h>

  207. int main(int argc, const char *argv[])
  208. {
  209.     char ch;
  210.     printf("ch=");
  211.     scanf("%c",&ch);
  212.     printf("%c\n",ch^0x20);
  213.     return 0;
  214. }
  215. 17.#使用位操作实现整数的乘法
  216. #include <stdio.h>

  217. int main(void)
  218. {
  219.     int m, n, i, result = 0;;

  220.     scanf("%d %d", &m, &n);

  221.     for(i = 0; i < 32; i++)
  222.         if((n >> i) & 0x01)
  223.             result += (m << i);
  224.     
  225.     printf("%d * %d = %d\n", m, n, result);
  226.     return 0;
  227. }
  228. 18.#位操作实现循环左移、右移
  229. #include <stdio.h>
  230. #include <stdlib.h>
  231. #include <string.h>

  232. unsigned shift_left(unsigned n, int bits)
  233. {
  234.     return (n << bits) | (n >> (32 - bits));
  235. }

  236. unsigned shift_right(unsigned n, int bits)
  237. {
  238.     return (n >> bits) | (n << (32 - bits));
  239. }

  240. int main(int argc, char *argv[])
  241. {
  242.     char *savep;

  243.     printf("left:0x%x\n", shift_left(strtol(argv[1], &savep, 16), atoi(argv[2])));
  244.     printf("right:0x%x\n", shift_right(strtol(argv[1], &savep, 16), atoi(argv[2])));
  245.     return 0;
  246. }
  247. 19.#使用位操作的知识先数组中保存26个没有重复的英文字母
  248. 方法1:
  249. #include <stdio.h>

  250. int main(int argc, const char *argv[])
  251. {
  252.     int flag=0,n,i;
  253.     char str[26];
  254.     srand(time(NULL));
  255.     while(flag!=0x3ffffff)
  256.     { n=rand()%26;
  257.         if(((flag>>n)&0x01)==0)
  258.         {
  259.             str[n]=n+'a';
  260.             flag|=0x01<<n;
  261.         }
  262.     }
  263.     for (i = 0; i < 26; i++)
  264.     {
  265.         printf("%c ",str[i]);
  266.     }
  267.     printf("\n");
  268.     return 0;
  269. }
  270. 方法2:
  271. #include <stdio.h>
  272. #include <stdlib.h>
  273. #include <time.h>

  274. int main(void)
  275. {
  276.     char letter[26];
  277.     int tmp, i, j, flag = 0;

  278.     srand(time(NULL));
  279.     for(i = 0; i < 26;){
  280.         tmp = rand() % 26;
  281.         if(((flag >> tmp) & 0x01) == 0){
  282.             letter[i++] = tmp + 'a';
  283.             flag |= (0x01 << tmp);
  284.         }
  285.         for(j = 31; j >= 0; j--)
  286.             printf("%d", (flag >> j) & 0x01);
  287.         printf("\n");
  288.     }

  289.     for(i = 0; i < 26; i++)
  290.         printf("%c ", letter[i]);
  291.     printf("\n");
  292.     return 0;
  293. }

阅读(1515) | 评论(0) | 转发(4) |
1

上一篇:结构体 _0215

下一篇:字符串操作 _0220

给主人留下些什么吧!~~