免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1729 | 回复: 2
打印 上一主题 下一主题

[C] 64位 位运算的问题 请教各位 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-13 16:16 |只看该作者 |倒序浏览
是想做个用8个字节的unsigned long 存8个小于255的无符整数的小函数工具
在移位的时候 发现和预期不一样  超过32位的移不上去  
请教各位  是我代码的问题  还是移位操作只能32位范围内

系统是64位的centos
  1. #include <stdio.h>

  2. unsigned long SetByteToLong(unsigned long i, unsigned char c, int pos)
  3. {
  4.         printf("i=%d (%.16x),c=%d,pos=%d\n", i , i, c ,pos);
  5.         unsigned long tmpl = -1;
  6.         printf("%.16x\n", tmpl);
  7.         unsigned long llll = ((unsigned long)c) << (8*(pos-1));
  8.         printf("%.16x\n", llll);
  9.         tmpl = tmpl & llll;
  10.         printf("%.16x\n", tmpl);
  11.         i = tmpl | i;
  12.         printf("%.16x, %.16x\n", i, i << 8);

  13.         return i;
  14. }


  15. int main()
  16. {
  17.         unsigned int aaa = 8;
  18.         unsigned long bbb = 0;

  19.         printf ("start:%d sizeof(unsigned long) =%d\n", aaa, sizeof(unsigned long));
  20.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 1);
  21.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 2);
  22.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 3);
  23.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 4);
  24.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 5);
  25.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 6);
  26.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 7);
  27.         bbb = SetByteToLong(bbb, (unsigned char)aaa, 8);
  28.         printf ("end:%x \n", bbb);

  29.         return 0;
  30. }
复制代码
执行结果
  1. start:8 sizeof(unsigned long) =8
  2. i=0 (0000000000000000),c=8,pos=1
  3. 00000000ffffffff
  4. 0000000000000008
  5. 0000000000000008
  6. 0000000000000008, 0000000000000800
  7. i=8 (0000000000000008),c=8,pos=2
  8. 00000000ffffffff
  9. 0000000000000800
  10. 0000000000000800
  11. 0000000000000808, 0000000000080800
  12. i=2056 (0000000000000808),c=8,pos=3
  13. 00000000ffffffff
  14. 0000000000080000
  15. 0000000000080000
  16. 0000000000080808, 0000000008080800
  17. i=526344 (0000000000080808),c=8,pos=4
  18. 00000000ffffffff
  19. 0000000008000000
  20. 0000000008000000
  21. 0000000008080808, 0000000008080800
  22. i=134744072 (0000000008080808),c=8,pos=5
  23. 00000000ffffffff
  24. 0000000000000000
  25. 0000000000000000
  26. 0000000008080808, 0000000008080800
  27. i=134744072 (0000000008080808),c=8,pos=6
  28. 00000000ffffffff
  29. 0000000000000000
  30. 0000000000000000
  31. 0000000008080808, 0000000008080800
  32. i=134744072 (0000000008080808),c=8,pos=7
  33. 00000000ffffffff
  34. 0000000000000000
  35. 0000000000000000
  36. 0000000008080808, 0000000008080800
  37. i=134744072 (0000000008080808),c=8,pos=8
  38. 00000000ffffffff
  39. 0000000000000000
  40. 0000000000000000
  41. 0000000008080808, 0000000008080800
  42. end:8080808
复制代码

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2012-09-13 16:26 |只看该作者
printf的格式问题,%x应改为 %lx,或者%llx

论坛徽章:
0
3 [报告]
发表于 2012-09-13 16:46 |只看该作者
   谢谢兄台
  1. start:8 sizeof(unsigned long) =8
  2. i=0 (0000000000000000),c=8,pos=1
  3. ffffffffffffffff
  4. 0000000000000008
  5. 0000000000000008
  6. 0000000000000008, 0000000000000800
  7. i=8 (0000000000000008),c=8,pos=2
  8. ffffffffffffffff
  9. 0000000000000800
  10. 0000000000000800
  11. 0000000000000808, 0000000000080800
  12. i=2056 (0000000000000808),c=8,pos=3
  13. ffffffffffffffff
  14. 0000000000080000
  15. 0000000000080000
  16. 0000000000080808, 0000000008080800
  17. i=526344 (0000000000080808),c=8,pos=4
  18. ffffffffffffffff
  19. 0000000008000000
  20. 0000000008000000
  21. 0000000008080808, 0000000808080800
  22. i=134744072 (0000000008080808),c=8,pos=5
  23. ffffffffffffffff
  24. 0000000800000000
  25. 0000000800000000
  26. 0000000808080808, 0000080808080800
  27. i=134744072 (0000000808080808),c=8,pos=6
  28. ffffffffffffffff
  29. 0000080000000000
  30. 0000080000000000
  31. 0000080808080808, 0008080808080800
  32. i=134744072 (0000080808080808),c=8,pos=7
  33. ffffffffffffffff
  34. 0008000000000000
  35. 0008000000000000
  36. 0008080808080808, 0808080808080800
  37. i=134744072 (0008080808080808),c=8,pos=8
  38. ffffffffffffffff
  39. 0800000000000000
  40. 0800000000000000
  41. 0808080808080808, 0808080808080800
  42. end:0808080808080808
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP