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)伪指 ...
随机推荐
- VM虚拟机,Linux系统安装tools过程遇到 what is the location of the “ifconfig” program
安装步骤: 复制到/mnt 解压文件 tar -zxvf VMwareTools-10.1.6-5214329.tar.gz 进入减压文件夹后安装 ./vmware-install.pl ... 一直 ...
- Android ADT插件更新后程序运行时抛出java.lang.VerifyError异常解决办法
当我把Eclipse中的 Android ADT插件从21.1.0更新到22.0.1之后,安装后运行程序抛出java.lang.VerifyError异常. 经过调查,终于找到了一个有效的解决办法: ...
- 简单搞懂OAuth2.0
本文转自:https://www.cnblogs.com/flashsun/p/7424071.html 原作者:闪客sun 一张图搞定OAuth2.0 目录 1.引言 2.OAuth2.0是什么 3 ...
- svn命令行
svn查看某一版本下的某一文件 svn cat -r 版本号 文件的目录 svn 对比两个版本之间的差别 svn diff -r 新版本:旧版本
- 微信小程序实战篇-下拉刷新与加载更多
下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 2. 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一个binds ...
- python字符编码与解码 unicode,str
解释以下几个问题: (1)python2中str和unicode是两种字符串类型,与字符编码方式是什么关系? (2)str和unicode是怎么相互转换的? (3)'\x...':'\u...', ' ...
- Centos7安装和配置Jira7.3.6
(1)安装jdk 1.下载jdk 链接:https://pan.baidu.com/s/1umdV-Cmm1wi1RP5clIJXmg 密码:zevc 2.安装jdk rpm -ivh jdk-8u1 ...
- 配置nginx作为下载站点
nginx默认情况是不允许列出整个目录浏览下载 1)autoindex参数详解 autoindex on //on开启目录浏览 autoindex_exact_size off; //off显示出文件 ...
- linux 挂载 ip-SAN
linux 挂载 ip-SAN 如何在 SAN 里面分配块就不讲了 # 安装 iscsi 工具 yum install iscsi-initiator-utils # 启动相关的服务 systemct ...
- Mysql数据库表的类型有哪些
截至目前,MySQL一共向用户提供了包括DBD.HEAP.ISAM.MERGE.MyIASM.InnoDB以及Gemeni这7种Mysql表类型.其中DBD.InnoDB属于事务安全类表,而其他属于事 ...