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)伪指 ...
随机推荐
- centos 引导盘
# grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6150 Vertex Cover 二分图,构造
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6150 题意:"最小点覆盖集"是个NP完全问题 有一个近似算法是说—每次选取度数最大 ...
- 小知识-为什么Linux不需要磁盘碎片整理
转载至:http://beikeit.com/post-495.html 简单译文: 这段linux官方资料主要介绍了外部碎片(external fragmentation).内部碎片(inter ...
- sharding-JDBC 实现读写分离
需求 一主两从,做读写分离. 多个从库之间实现负载均衡. 可手动强制部分读请求到主库上.(因为主从同步有延迟,对实时性要求高的系统,可以将部分读请求也走主库) 本次不讨论 MySQL如何配置主从同步相 ...
- K8s的内部Pod之间都不通,搞了快两天
试了不亚于二十种方法,绝望的时候,回到了家. 想手工安装,又遇到flannel在手工下,会更改docker启动项的不完善. cni,或许就是k8s的大方向吧. 最后,抱着试一试的态度,将flannel ...
- 在C#中使用正则表达式筛选出图片URL并下载图片URL中的图片到本地
本功能主要用到的知识点如下: 1.正则表达式 2.C#中下载文件功能的实现 3.泛型集合的使用 4.进程的简单操作(用于结束当前程序) 下面就简单说一下是如何使用这些知识点的.先详细说下这个程序主要实 ...
- Rsync+Inotify 搭建实时同步数据
1.安装软件包 # yum install inotify-tools # yum -y install rsync 2.同步机器相互添加信任 [root@host-10-0-100-106 ~]# ...
- C# For Bot Framework
Bot Framework是一个聊天机器人的框架,背后是微软的SDK,它可以使用C#和Nodejs开发,今天我尝试用创建一个比较简单Bot 参考地址:https://docs.microsoft.co ...
- 洛谷P2278 [HNOI2003] 操作系统
题目传送门 分析:题目中提到了优先级,很显然这题要用优先队列+模拟.题目中很多细节需要注意,还是在代码中解释吧,这里我用的是手打的堆. Code: #include<bits/stdc++.h& ...
- Codeforces Round #300 Quasi Binary(DP)
Quasi Binary time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...