1,多个字节的数据赋值给单个字节

#include<stdio.h>typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;int main(void)
{printf("u32 is %d byte(s)\t u16 is %d byte(s)\t u8 is %d byte(s)\n",sizeof(u32),sizeof(u16),sizeof(u8));u32 intNum = 0x11223344;u16 shortNum = 0x5566;u8  charNum = 0x77;charNum = intNum;printf("The charNum is %x.\t", charNum); charNum = shortNum;printf("Now the charNum is %x.\t", charNum);return 0;}

运行结果:

结论:多字节数据作为左值赋给单字节的右值时,由于单字节的“容量”不足以存放多字节,数据截断到可以存放为止,目前的标准是存放最低位到足够位置存放。所以u32的0x11223344赋给u8的charNum时,相较截断了高位的0x112233,把0x44赋给了charNum;同理0x5566的0x55也被截断,只留低位的0x66被赋给charNum。

2,单字节数组的数据赋值给多字节数据

也可以把u32的数据看成是4个u8数据组成的数组,u16看成2个u8

#include<stdio.h>typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;int main(void)
{int i = 0;u32 intNum = 0x11223344;u16 shortNum = 0x5566;u8  charNum[4] = {0x77,0x88,0x99,0xAA};u8  charNum1[2] = { 0xbb,0xcc };/* output the primary data of array charNum and charNum1 */printf("output the primary data of array charNum and charNum1\n");for ( i = 0;i < sizeof(charNum);i++){printf("The charNum[%d] is %x.",i, charNum[i]);}printf("\n");for (i = 0;i < sizeof(charNum1);i++){printf("The charNum1[%d] is %x.", i, charNum1[i]);}printf("\n\n");/* output the shifted data of array charNum and charNum1 */printf("output the shifted data of array charNum and charNum1\n");charNum[0] = ((u32)intNum>>24);     //0x11  = 0x00000011 = 0x11223344>>24   charNum[1] = ((u32)intNum >> 16);   //0x22  = 0x00001122 = 0x11223344>>16  charNum[2] = ((u32)intNum >> 8);    //0x33  = 0x00112233 = 0x11223344>>8   charNum[3] =       intNum;          //0x44  = 0x11223344 = 0x11223344for (i = 0;i < sizeof(charNum);i++){printf("The charNum[%d] is %x.", i, charNum[i]);}printf("\n");charNum1[0] = ((u16)shortNum >> 8); //0x55 = 0x0055 = 0x5566>>8 charNum1[1] = shortNum;      //0x66 = 0x5566for (i = 0;i < sizeof(charNum1);i++){printf("The charNum[%d] is %x.", i, charNum1[i]);}printf("\n");return 0;}

运行结果:

结论:多字节数据赋值给等值单字节数组时,会使用右移将多字节数据移到最低位字节,就像这样下面这句,移位后打印的charNum[0]为0x11;

charNum[0] = ((u32)intNum>>24); //0x11223344>>24  0x00000011

当移位后除最低位字节以外的字节仍然有非0数据时也不用担心,它会因为内存不够的关系被截断,而只剩下最低位字节数据。例如这句,移位后打印的charNum[2]为0x33。

charNum[2] = ((u32)intNum >> 8);  //0x11223344>>8 0x00112233

3,多字节数据赋值给单字节数组的数据

#include<stdio.h>typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;int main(void)
{int i = 0;u32 intNum = 0x11223344;u16 shortNum = 0x5566;u8  charNum[4] = {0x77,0x88,0x99,0xAA};u8  charNum1[2] = { 0xbb,0xcc };/* output the primary data of intNum */printf("output the primary data of charNum and charNum1\n");printf("The intNum is %x, shortNum is %x.", intNum, shortNum);printf("\n\n");/* output the shifted data of intNum */printf("output the shifted data of intNum and shortNum\n");intNum = ((u32)charNum[0] << 24) | ((u32)charNum[1] << 16) | ((u32)charNum[2] << 8) | charNum[3];
//0x77000000 = 0x00000077<<24  0x77880000 = 0x00000088<<16  0x77889900 = 0x00000099<<8 0x778899AA = 0x000000AA<<24  printf("The intNum is %x\n", intNum);shortNum = ((u16)charNum1[0] << 8) + charNum1[1];//0xBB00 = 0x00BB<<8     0xBBCC = 0x00CCprintf("The shortNum is %x\n", shortNum);return 0;}

运行结果:

结论:单字节数组赋值给多字节数据时,采用左移,将单字节数据移到多字节对应位的位置。另外上面的例程也说明了对数组的运算,“+”和“|”运算机制不一样,但结果是一样的。因为目标操作位的位置正好是0,所以结果一样,如果目标操作位有数据时,则会体现2种运算的差异,例如

printf("%d  %d   %d   %d ", (2 + 3), (2 | 3), (2 | 8), (2 + 8));

运行结果:

PS:如果用多个表达式进行移位赋值,像这样

#include<stdio.h>typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;int main(void)
{int i = 0;u32 intNum = 0x11223344;u16 shortNum = 0x5566;u8  charNum[4] = {0x77,0x88,0x99,0xAA};u8  charNum1[2] = { 0xbb,0xcc };/* output the primary data of intNum */printf("output the primary data of charNum and charNum1\n");printf("The intNum is %x, shortNum is %x.", intNum, shortNum);printf("\n\n");/* output the shifted data of intNum */printf("output the shifted data of intNum and shortNum\n");/*   expect                            real                       */intNum = ((u32)charNum[0] << 24); //0x77000000 = 0x00000077<<24        0x77000000 = 0x00000077<<24intNum = ((u32)charNum[1] << 16);  //0x77880000 = 0x00000088<<16        0x00880000 = 0x00000088<<16intNum = ((u32)charNum[2] << 8);   //0x77889900 = 0x00000099<<8     0x00009900 = 0x00000099<<8intNum =       charNum[3];            //0x778899AA = 0x000000AA<<24        0x000000AA = 0x000000AA<<24//intNum = ((u32)charNum[0] << 24) | ((u32)charNum[1] << 16) | ((u32)charNum[2] << 8)| charNum[3];printf("The intNum is %x\n", intNum);/*   expect                            real                        */shortNum = ((u16)charNum1[0] << 8);       //0xBB00 = 0x00BB<<8              0xBB00 = 0x00BB<<8shortNum = charNum1[1];                  //0xBBCC = 0x00CC                0x00CC = 0x00CC//shortNum = ((u16)charNum1[0] << 8) + charNum1[1];printf("The shortNum is %x\n", shortNum);return 0;}

运行结果:

结论:得到目标数据只是最后一次赋值的结果,而不是4(2)次赋值的总和。

1个字节,2个字节,4个字节数据之间赋值相关推荐

  1. MySQL宽字节注入漏洞分析_宽字节注入

    概念 单字节字符集: 所有的字符都使用一个字节来表示,比如 ASCII 编码. 多字节字符集: 在多字节字符集中,一部分字节用多个字节来表示,另一部分(可能没有)用单个字节来表示. 两位的多字节字符有 ...

  2. java 英文字符 字节_3、在JAVA语言中,每个英文字符占 个字节,每个中文汉字占( )个字节。...

    [判断题]中心原子中的几个原子轨道杂化时,必形成数目相同的杂化轨道. [单选题]集合 用区间表示正确的是 ( ) [单选题]15.Java语言的类间的继承关系是 [单选题]8.编译Java Appli ...

  3. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个...

    2019独角兽企业重金招聘Python工程师标准>>> 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,如"我ABC ...

  4. java byter是字节吗_GitHub - XXQAQ/Byter: 字节对象转换框架,一个基于字节的 Gson/FastJson...

    Byter 字节对象转换框架,一个基于字节的 Gson/FastJson 众所周知,Json数据的序列化内容就是字符串,如果说Gson/FastJson是基于字符串的Json转化框架,那么Byter就 ...

  5. 网络通信之 字节序转换原理与网络字节序、大端和小端模式

    一.在进行网络通信时是否需要进行字节序转换? 相同字节序的平台在进行网络通信时可以不进行字节序转换,但是跨平台进行网络数据通信时必须进行字节序转换. 原因如下:网络协议规定接收到得第一个字节是高字节, ...

  6. java 字节取位_java位 、字节 、字符的梳理

    1字节(byte)=8位(bit) char=2字节(这是因为char是Java中的保留字,Java用的是Unicode,所以char在Java中是16位即2个字节的.) 附: String str= ...

  7. java 压缩汉字字节_java中计算汉字的字节数

    中文并不一定是占两个字节的,具体占多少字节是跟具体的编码方式相关的. 比如说:GB2312.GBK.GB18030 编码是占用两个字节的,但是 UTF-8 编码的话至少需要占用三个字节. 有一个简单方 ...

  8. 编写一个程序,实现将存放在AX和DX中的32位数据循环右移二进制数的4位。(DX存放高字节内容,AX存放低字节内容)

    编写一个程序,实现将存放在AX和DX中的32位数据循环右移二进制数的4位.(DX存放高字节内容,AX存放低字节内容) P151 例4.9 汇编思路: AX右移四位后,使用BH接收AL的低四位数据,得到 ...

  9. java中字节码_聊聊Java的字节码

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 巴山楚水凄凉地,二十三年弃置身. 怀旧空吟闻笛赋,到乡翻似烂柯人. 沉舟侧畔千帆过,病树前头万 ...

最新文章

  1. Java中各种集合特点总结
  2. android 应用uid,android adb 获取所有app 的uid
  3. springboot项目中使用日志
  4. why the SalesOrder header note is read only
  5. 在控制器控制方式中,异步控制与联合控制有什么区别?
  6. CLR寄宿(上) MSCOREE.DLL
  7. OCS 2007 R2搭建准备虚机及快照
  8. MapGuide安装
  9. pytorch 提取权重_获取Pytorch中间某一层权重或者特征
  10. cur前缀_每个人必知的英语前缀大全
  11. unity步步生花(触发类互动)
  12. 京东云主机 mysql_京东云所有地域正式支持 MySQL 8.0!
  13. 安警官的IP地址是怎样定位到莽村附近的?
  14. Java中outer标签的用法
  15. Rockchip_Developer_Guide_USB_FFS usb测试demo
  16. html页面放大缩小样式不变,网页缩小放大后错位的解决方法
  17. linux查看cp2102,微雪电子CP2102(type A)USB转USART简介
  18. 一个菜鸟程序员的蜕变史
  19. openwrt的luci应用ipk包开发(二)
  20. @Scheduled用法

热门文章

  1. iPad与电脑端文件互传解决方案(基于nPlayer lite)
  2. 用T-SQL语句创建数据库
  3. java解析文件_Java读取文件方法大全
  4. 电脑系统重装win10的方法教程,win10系统重装教程
  5. 易语言下载到c盘文件覆盖,易语言写到文件不覆盖
  6. 反渗透设备:反渗透纯水设备中软水器的作用及维护方法
  7. 银联前置的一些基本知识
  8. 2022-2027年中国羽绒行业市场全景评估及发展战略规划报告
  9. FTP登录提示421 Service not available
  10. 关于Python自动化操作Excel的36个Python函数【面试必学】