鱼C论坛

 找回密码
 立即注册
查看: 1557|回复: 3

[技术交流] 位移

[复制链接]
发表于 2020-4-25 11:40:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
c:
  1. #include <stdio.h>
  2. #include <string.h>
  3. void print_byte_content(int);
  4. int main()
  5. {

  6.         int value;
  7.         short before=1920;
  8.         short after=1080;
  9.         memmove((char*)&value,(char*)&before,2);//低位
  10.         memmove((char*)&value+2,(char*)&after,2);//高位
  11.        
  12.         print_byte_content(before);
  13.         print_byte_content(after);
  14.         print_byte_content(value);
  15.        
  16.         int width=value<<16;//低位往高位移
  17.         print_byte_content(width);
  18.         width=width>>16;//高位往低位移
  19.         print_byte_content(width);
  20.        
  21.         int height=value>>16;
  22.         print_byte_content(height);
  23.         return value;
  24. }

  25. void print_byte_content(int data)
  26. {       
  27.         int len;
  28.         int byte;
  29.         printf("int %d 内存地址上的内容为:\n",data);
  30.         for (len=0;len<sizeof(data);len++)
  31.         {
  32.                 printf("%d:\t",len);
  33.                 for (byte=7;byte>=0;byte--)
  34.                         printf("%d",(*((char*)&data+len)&(1<<byte)) != 0);
  35.                 putchar('\n');
  36.         }
  37. }
复制代码

bat内容:
  1. @echo off
  2. 0410.exe

  3. set value=%errorlevel%
  4. set /a height="value>>16"
  5. set /a width="value<<16"
  6. set /a width="width>>16"

  7. echo height=%height%
  8. echo width=%width%
  9. pause
复制代码

结果:
  1. int 1920 内存地址上的内容为:
  2. 0:      10000000
  3. 1:      00000111
  4. 2:      00000000
  5. 3:      00000000
  6. int 1080 内存地址上的内容为:
  7. 0:      00111000
  8. 1:      00000100
  9. 2:      00000000
  10. 3:      00000000
  11. int 70780800 内存地址上的内容为:
  12. 0:      10000000
  13. 1:      00000111
  14. 2:      00111000
  15. 3:      00000100
  16. int 125829120 内存地址上的内容为:
  17. 0:      00000000
  18. 1:      00000000
  19. 2:      10000000
  20. 3:      00000111
  21. int 1920 内存地址上的内容为:
  22. 0:      10000000
  23. 1:      00000111
  24. 2:      00000000
  25. 3:      00000000
  26. int 1080 内存地址上的内容为:
  27. 0:      00111000
  28. 1:      00000100
  29. 2:      00000000
  30. 3:      00000000
  31. height=1080
  32. width=1920
  33. 请按任意键继续. . .
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-26 07:34:39 | 显示全部楼层
  1. printf("%d",(*((char*)&data+len)&(1<<byte)) != 0);
复制代码


这段看不太懂,请指点一下。

另外:
  1. int width=value<<16;//低位往高位移
复制代码


这句在我电脑上编译出错是怎么回事呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-26 08:03:29 | 显示全部楼层
SugarCane88 发表于 2020-4-26 07:34
这段看不太懂,请指点一下。

另外:

将【*((char*)&data+len)】这个数与【(1<<byte)】按位与,只有两个比特位都为1结果为1,1 != 0成立,打印1,相反为0;这样就是输出了1个比特位的值,循环8次【(1<<byte)】,每个位都有做比较!

编译器报错,你可以把你的报错信息贴出来看看。我用的devc++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-26 08:17:31 | 显示全部楼层
Cool_Breeze 发表于 2020-4-26 08:03
将【*((char*)&data+len)】这个数与【(1

谢谢您的回复,我用的是VC6.0,估计编程环境的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 09:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表