C语言中的位域、字节序、比特序、大小端
转:http://www.360doc.com/content/13/0624/10/496343_295125641.shtml
我们知道一个字节有8位,也就是8个比特位。从第0位到第7位共8位。比特序就是用来描述比特位在字节中的存放顺序的。通过阅读网页http://en.wikipedia.org/wiki/Bit_numbering的内容,关于比特序我们得到下面的结论:
| Endian | First Byte (lowest address) |
Middle Bytes | Last Byte (highest address) |
Summary |
|---|---|---|---|---|
| big | most significant | ... | least significant | Similar to a number written on paper (in Arabic numerals) |
| little | least significant | ... | most significant | Arithmetic calculation order (see carry propagation) |
Atomic element size 8-bit, address increment 1-byte (octet)
| increasing addresses → | |||||
| ... | 0Ah | 0Bh | 0Ch | 0Dh | ... |
The most significant byte (MSB)
value, which is 0Ah in our example, is stored at the memory location
with the lowest address, the next byte value in significance, 0Bh, is
stored at the following memory location and so on. This is akin to
Left-to-Right reading in hexadecimal order.
Atomic element size 16-bit
| increasing addresses → | |||||
| ... | 0A0Bh | 0C0Dh | ... | ||
The most significant atomic element stores now the value 0A0Bh, followed by 0C0Dh.
Little-endian
Atomic element size 8-bit, address increment 1-byte (octet)
| increasing addresses → | |||||
| ... | 0Dh | 0Ch | 0Bh | 0Ah | ... |
The least significant byte (LSB) value, 0Dh, is at the lowest address. The other bytes follow in increasing order of significance.
Atomic element size 16-bit
| increasing addresses → | |||||
| ... | 0C0Dh | 0A0Bh | ... | ||
The least
significant 16-bit unit stores the value 0C0Dh, immediately followed
by 0A0Bh. Note that 0C0Dh and 0A0Bh represent integers, not bit layouts
(see bit numbering).
| ← increasing addresses | |||||
| ... | 0Ah | 0Bh | 0Ch | 0Dh | ... |
The least significant byte (LSB) value, 0Dh, is at the lowest address. The other bytes follow in increasing order of significance.(这个明显符合我们的习惯)
With 16-bit atomic elements:
| ← increasing addresses | |||||
| ... | 0A0Bh | 0C0Dh | ... | ||
The least significant 16-bit unit stores the value 0C0Dh, immediately followed by 0A0Bh.
The display of
text is reversed from the normal display of languages such as English
that read from left to right. For example, the word "XRAY" displayed in
this manner, with each character stored in an 8-bit atomic element:
| ← increasing addresses | |||||
| ... | "Y" | "A" | "R" | "X" | ... |
If pairs of characters are stored in 16-bit atomic elements (using 8 bits per character), it could look even stranger:
| ← increasing addresses | |||
| ... | "AY" | "XR" | ... |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- char a[] = {'a', 'b', 'c'};
- char b[] = {'d', 'e', 'f'};
- a[3] = 0;
- printf("strlen(a)=%d, strlen(b)=%d\n", strlen(a), strlen(b));
- printf("a=%s, b=%s\n", a, b);
- printf("sizeof(a)=%d, sizeof(b)=%d\n", sizeof(a), sizeof(b));
- return 0;
- }
a=abc, b=defabc
sizeof(a)=3, sizeof(b)=3
- #include <stdio.h>
- int main()
- {
- unsigned long array[] = {0x12345678, 0xabcdef01, 0x456789ab};
- unsigned short ret;
- ret = *((unsigned short *)((unsigned long)array+7));
- printf("0x%x\n", ret);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main(void){
- int a[5]={1,2,3,4,5};
- int *ptr =(int *)(&a+1);
- printf("%d,%d\n",*(a+1),*(ptr-1))
- return 0;
- }
- #include <stdio.h>
- #include <assert.h>
- int main()
- {
- unsigned short x = 0xff01;
- assert(sizeof(x) >= 2);
- if(*(char*)&x == 1) //if(char(x) == 1)
- printf("little-endian\n");
- else if((char)x > 1)
- printf("big-endian\n");
- else
- printf("unknown\n");
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- union{
- char c;
- int i;
- }u;
- u.i = 0x0201;
- if(u.c == 1)
- printf("little-endian\n");
- else if(u.c == 2)
- printf("big-endian\n");
- else
- printf("unknown\n");
- return 0;
- }
- #include <stdio.h>
- union u{
- struct {
- char i:1;
- char j:2;
- char m:3;
- } s;
- char c;
- }r;
- int main()
- {
- r.s.i = 1; // 1
- r.s.j = 2; // 10
- r
- printf("0x%x\n", r.c);
- return 0;
- }
- #include <stdio.h>
- union {
- struct
- {
- unsigned char a1:2;
- unsigned char a2:3;
- unsigned char a3:3;
- }x;
- unsigned char b;
- }d;
- int main(int argc, char* argv[])
- {
- d0 0100
- printf("0x%x\n0x%x\n0x%x\n", d.x.a1, d.x.a2, d.x.a3);
- return 0;
- }
似乎也符合:小端CPU通常采用的是LSB 0 位序 的惯例。
因为前面我们说过:“但是大端CPU却有可能采用LSB 0 位序也有可能采用的是MSB 0 位序”
C语言中的位域、字节序、比特序、大小端的更多相关文章
- C语言中的位域的使用
转载:http://blog.sina.com.cn/s/blog_648d306d0100mv1c.html C语言中的位域的使用一.位域 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几 ...
- C语言中的位域[转]
有些信息在存储时,并不需要占用一个完整的字节,而只需要一个或几个二进制位即可;比如:在存放一个开关量时,只有0和1两种状态,只需要使用一个二进制位即可存储;为了节省存储空间,C语言提供了一种数据结构, ...
- 关于C语言中的位域
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态,用一位二进位即可.为了节省存储空间,并使处理简便,C语言提供了一种数据结构,称 ...
- C语言中数据类型的字节数
类型 16位 32 位 64位 char 1 1 1 short int 2 2 2 int 2 4 4 unsigned int 2 4 4 float 4 4 4 double 8 8 8 lon ...
- C语言判断大小端的几种方法
在操作系统中,经常会用到判断大小端,很多面试题中也会经常遇到,以前的时候没有总结过,这里总结一下. 以后用到了就直接可以用了. 所谓的大小端,大致的解释意思就是: [大端模式] CPU对操作数的存放方 ...
- c语言:union,大小端
union: 不允许只用联合变量名作赋值或其它操作. 也不允许对联合变量作初始化赋值,赋值只能在程序中进行. 小端存储: 以字节为单位,低存低,高存高. 任何数据在内存中都是以二进制(1或着0)顺序存 ...
- 用C语言,如何判断主机是 大端还是小端(字节序)
所谓大端就是指高位值在内存中放低位地址,所谓小端是指低位值在内存中放低位地址.比如 0x12345678 在大端机上是 12345678,在小端机上是 78564312,而一个主机是大端还是小端要看C ...
- C语言中的字节对齐以及其相关处理
首先,我们来了解下一些基本原理: 一.什么是字节对齐一个基本类型的变量在内存中占用n个字节,则该变量的起始地址必须能够被n整除,即: 存放起始地址 % n = 0,那么,就成该变量是字节对齐的;对于结 ...
- C语言中的字节对齐
下面这个篇博客讲解很好 http://blog.csdn.net/meegomeego/article/details/9393783 总的来看分三类: 1. 不加 #pragma pack(n)伪指 ...
随机推荐
- webapi-1 给现有MVC 项目添加 WebAPI
1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...
- canvas写的地铁地图
更新: 18-9-21:填了个坑,更新了canvas绘制过程. 根据的是百度提供的坐标,canvas的坐标是大的坐标在后面,所以跟实际生活方向相反. 所以canvas里的北方在下方,实际生活中北方在上 ...
- EXT入门学习
今天,对EXT做了一下初步的了解,了解了一些基本用的函数.窗体对象.表单.文本域.按钮,一些基本的函数我列了出来,写了个登陆的demo,是根据别人的例子模仿出来的,见谅哈. 基本函数 Ext.onRe ...
- php文件上传错误信息
错误信息说明 UPLOAD_ERR_OK:其值为0,没有错误发生,文件上传成功 UPLOAD_ERR_INI_SIZE:其值为1,上传的文件超过了php.ini和upload_max_filesize ...
- IIS 7浏览网站出错,错误编码 http 503 service unable
由于重新装了Microsoft Visual Studio 2010,结果运行出错,检查发现应用程序池该项目已停止. 解决办法: 1.打开IIS:开始菜单>运行 2.输入“inetmgr”回车 ...
- windows查看指定端口
- 六十 数据库访问 使用SQLAlchemy
数据库表是一个二维表,包含多行多列.把一个表的内容用Python的数据结构表示出来的话,可以用一个list表示多行,list的每一个元素是tuple,表示一行记录,比如,包含id和name的user表 ...
- Ionic-wechat项目边开发边学(三):自定义样式,指令,服务
摘要 上一篇文章主要介绍了一个ionic项目的标准目录结构,header标签的使用,以及页面之间的切换.这篇文章实现的功能有: 消息数据的获取, 消息列表的展示, 消息标为已读/未读, 主要涉及的到的 ...
- asp.net mvc 中使用NPOI导出excel
版本信息:NPOI1.2.5(2.0以上的版本很多方法不清楚) 明确三点: path: mvc 部署网站的时候,我们肯定要拷贝的一个文件夹就mvc的UI层,有点可以肯定的是,你部署网站的路径不一定都是 ...
- Java网络编程一
1.InetAddress的应用 import java.util.List; import java.math.BigDecimal; import java.net.InetAddress; im ...