国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

大小端 位域 字節對齊

2019-11-06 07:18:57
字體:
來源:轉載
供稿:網友

一、大小端:

1 大端: 數據低位存放在內存高地址, 數據高位存放在內存低地址

2 小端: 數據低位存放在內存底地址, 數據高位存放在內存高地址

3 網絡字節序以大端傳輸、主機字節序根據CPU架構不同既有大端與有小端

以聯合體來做說明:

union endpoint {	unsigned short n;	char ch[2];};int main(){	endpoint endpoint_t;	endpoint_t.n = 0x1122;	if (endpoint_t.ch[0] == 0x22 && endpoint_t.ch[1] == 0x11)		std::cout << "little ending" << std::endl;	else		std::cout << "big ending" << std::endl;
	return 0;}

/*

二、位域已知一個16位的整數,請按4位為一個數, 寫個求和函數。如:16位整數的二進制為1101 0101 1110 0011, 計算結果為 1101 + 0101 + 1110 + 0011 = 35

*/

union endpoint {	unsigned short n;	char ch[2];};typedef struct set_ {#ifndef __LITTLE_ENDING__	unsigned short a : 4;	unsigned short b : 4;	unsigned short c : 4;	unsigned short d : 4;#else	unsigned short c : 4;	unsigned short d : 4;	unsigned short a : 4;	unsigned short b : 4;#endif}set_t;unsigned short binset_plus(const unsigned short n){	set_t s = { 0 };	memcpy(&s, &n, sizeof(short));	return s.a + s.b + s.c + s.d;}void main(){	endpoint endpoint_t;	endpoint_t.n = 0x1122;	if (endpoint_t.ch[0] == 0x22 && endpoint_t.ch[1] == 0x11)		std::cout << "little ending" << std::endl;	else		std::cout << "big ending" << std::endl;	unsigned short n = 0xD5E3;  // 1101 0101 1110 0011	unsigned short na = binset_plus(n);}/*

三、字節對齊

* /

void main()

{	struct node {	};	unsigned short n = sizeof(node);  // 1	struct node1 {		int a;		char b;		short c;	};	n = sizeof(node1);  //因為結構體node1中最長的數據類型是int,占4個字節,因此以4字節對齊,則該結構體在內存中存放方式為						//|--------int--------|						//|char|----|--short--|						// 4 + 4 = 8	struct node2 {		char b;		int a;		short c;	};	n = sizeof(node2);  //因為結構體node2中最長的數據類型是int,占4個字節,因此以4字節對齊,則該結構體在內存中存放方式為						//|-------char--------|						//|--------int--------|						//|-------short-------|						// 4 + 4 + 4= 12	struct node4 {		bool a;		node1 node1_;		short b;	};	n = sizeof(node4); //4 + 8 + 4 因為node1中最寬字段為int a, 占4字節且小于默認的8字節對齊, 所以取4byte為對齊字節	struct node5 {		bool a;		node1 nodde_;		char d;		double b;		char c;	};	n = sizeof(node5);  //8 + 8 + 8 + 8 + 8 = 40#PRagma pack(4)	struct node6 {		bool a;		node1 nodde_;		char d;		double b;		char c;	};	n = sizeof(node6);  //4 + 8 + 4 + 8 + 4 = 28#pragma pack()}


上一篇:jvm基礎

下一篇:怎樣寫一個lemon的spj

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆化县| 蓬莱市| 尤溪县| 巴塘县| 工布江达县| 瓮安县| 遂宁市| 司法| 陵水| 德格县| 阿荣旗| 斗六市| 忻城县| 霍林郭勒市| 龙山县| 四子王旗| 和硕县| 呼伦贝尔市| 建昌县| 长寿区| 巴马| 盐津县| 公主岭市| 梓潼县| 收藏| 三江| 永康市| 颍上县| 万年县| 来安县| 通渭县| 晴隆县| 金堂县| 莱州市| 盐津县| 策勒县| 陈巴尔虎旗| 和政县| 清涧县| 天峻县| 车致|