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)伪指 ...
随机推荐
- Timer类
构造方法:public Timer() 创建一个新计时器.相关的线程不 作为守护程序运行. public Timer(boolean isDaemon) 创建一个新计时器,可以指定其相关的线程作为守护 ...
- C后端设计开发 - 第5章-内功-数据结构下卷
正文 第5章-内功-数据结构下卷 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了.
- mui页面跳转
$('.mui-title').on('click',function(){ mui.openWindow({ //跳转到指导信息页面 url:"/index.php?m=mobile&am ...
- hibernate连接Oracle rac
连接方式与普通的数据库不一样.connection.url 中使用了LOAD-BALANCE = yes 要不然会报错 <hibernate-configuration> <sess ...
- UVALive 5099
B - Nubulsa Expo Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- HUD-5379
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- redis之(十一)redis实现缓存的功能
[一]redis实现缓存的原理 --->利用键的失效时间设置实现缓存技术 --->由于redis的内存有限,可以在redis的配置文件里设置maxmemory的参数.来限制redis最大可 ...
- golang实现mysql数据库备份
背景 navicat是mysql可视化工具中最棒的,但是,在处理视图的导入导出方面,它是按照视图名称的字母顺序来处理的,若视图存在依赖,在导入过程中就会报错.前面已经用python写了一个,但在使用过 ...
- requere.js优化js脚本加载方案,使用篇。
require.config({ paths: { "jquery": "jquery-3.2.1", 'index':"index" }} ...
- Mac安装Maven
1.从官网(https://maven.apache.org/download.cgi)下载 Maven 并解压. 2.配置环境 . vim ~/.bash_profile export MAVEN ...