日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

位域 内存 字节序_C语言中的位域、字节序、比特序、大小端(转)

發(fā)布時(shí)間:2023/12/19 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 位域 内存 字节序_C语言中的位域、字节序、比特序、大小端(转) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.比特序

/ 位序 /?bit numbering

/

我們知道一個(gè)字節(jié)有8位,也就是8個(gè)比特位。從第0位到第7位共8位。比特序就是用來描述比特位在字節(jié)中的存放順序的。通過閱讀網(wǎng)頁的內(nèi)容,關(guān)于比特序我們得到下面的結(jié)論:

(1)比特序分為兩種:LSB

0位序和MSB

0位序。

LSB

0位序是指:字節(jié)的第0位存放數(shù)據(jù)的,即我們的數(shù)據(jù)的最低位存放在字節(jié)的第0位。

MSB

0位序是指:字節(jié)的第0位存放,即我們的數(shù)據(jù)的最高位存放在字節(jié)的第0位。

所以說對(duì)于代碼:char *ch = 0x96;

// ?0x96 = 1001 0110

指針ch到底指向哪里呢?不難知道,如果是LSB

0位序則顯然指針ch指向最右邊的也是最低位的0.

而如果是MSB

0位序則顯然指針ch指向最左邊的也是最高位的1.

LSB 0: A container for

8-bit binary number with the highlighted?least significant bit?assigned the

bit number 0

MSB 0:A container for 8-bit

binary number with the highlighted?most significant bit?assigned the bit

number 0

(2)小端CPU通常采用的是LSB

0位序,但是大端CPU卻有可能采用LSB

0位序也有可能采用的是MSB

0位序

(Little-endian?CPUs usually employ

"LSB 0" bit numbering, however both bit numbering conventions can

be seen in?big-endianmachines. )

(3)推薦的標(biāo)準(zhǔn)是MSB

0位序。

(The

recommended style for?Request for Comments?documents is

"MSB 0" bit numbering.)

(4)Bit

numbering is usually transparent to the

In?computing, the

term?endian?or?endianness?refers

to the ordering of individually addressable sub-components within

the representation of a larger data item as stored

in?external memory?(or,

sometimes, as sent on a serial connection). Each sub-component in

the representation has a unique degree of significance, like

the?place

value?of digits in a decimal number. These

sub-components are typically 16- or

32-bit?words, 8-bit?bytes, or

even?bits.

Endianness is a difference in data representation at the hardware

level and may or may not be transparent at higher levels, depending

on factors such as the type of high level language used.

計(jì)算機(jī)中,術(shù)語“端”是指:在內(nèi)存中的一個(gè)較大的數(shù)據(jù),它是由各個(gè)可以被單獨(dú)尋址的部分組成,這些組成部分在該數(shù)據(jù)中是以怎樣的順序存放的呢?而這個(gè)問題涉及到“端”的概念,CPU是大端還是小端決定了這些組成部分的存放順序。

這些組成部分可能是16或32位的字、8位的字節(jié)、甚至是比特位。

The most common cases refer

to how bytes are ordered within a single。

我們通常碰到的情況是:字節(jié)是以怎樣的順序存放在一個(gè)16、32、64位的數(shù)據(jù)中。

(當(dāng)我們要存取一個(gè)16、32、64位數(shù)據(jù)的某一組成部分,也就是某一個(gè)或幾個(gè)字節(jié)時(shí),就要特別注意機(jī)器的“大小端”)

A?big-endian?machine

stores the?most?significant byte

first, and

a?little-endian?machine stores

the?least?significant byte

first.

Quick Reference - Byte

Machine Example

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

little

least?significant

...

most?significant

Arithmetic calculation

order (see

Examples of storing the

value?0A0B0C0Dh?in

memory

Big-endian

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

很顯然“小端”機(jī)器符合“高高低低”的原則。及高位字節(jié)或字存放在高地址,低位字節(jié)或字存放在低地址。

另外“小端”機(jī)器中,數(shù)據(jù)在CPU的寄存器和內(nèi)存中的存放順序是一致的。

Byte addresses increasing

from right to left

在我們寫: 0xFF86 時(shí),很明顯地址是從右向左遞增的。也就是低位寫在右邊,高位寫在左邊。

但是當(dāng)我們寫字符串時(shí):char *str = "Hello

world!",卻是低位的字符寫在左邊,高位的字符寫在了右邊。

With 8-bit atomic

elements:

←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.(這個(gè)明顯符合我們的習(xí)慣)

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"

...

(可以看到和我們手寫的順序是相反的,這一點(diǎn)特別要注意!)

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"

...

相關(guān)的一個(gè)C例子:

#include

#include

#include

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;

}

運(yùn)行結(jié)果:

strlen(a)=3,

strlen(b)=6

a=abc, b=defabc

sizeof(a)=3, sizeof(b)=3

分析:

字符數(shù)組a和b都分配在棧上,先分配a,

而a中的字符是如何分配的呢?顯然因?yàn)椤皩懽址畷r(shí),低位的字符寫在左邊,高位的字符寫在了右邊”。'a'是最低位,'b'在中間,而'c'在最高位。而棧是從高地址從低地址擴(kuò)展的。假如是小端CPU的話,按照“高高低低”的原則,高位的'c'應(yīng)該最先分配,接著是'b',最后是'a'。

分配玩字符數(shù)組a之后,在分配字符數(shù)組b,同樣的道理,高位的'f'應(yīng)該最先分配,接著是'e',最后是'd'。

再執(zhí)行a[3] = 0;顯然a[3]的地址應(yīng)該比'c'字符的地址要高。所以該語句執(zhí)行玩之后的棧的情況如下:

高地址 <

\0?c?b?a?f?e?d

所以:a字符串打印的結(jié)果是:abc,而b字符串打印的結(jié)果是:defabc.

strlen函數(shù)是計(jì)算字符串的長度,當(dāng)然要找到最后的結(jié)束字符'\0',才停止計(jì)算。所以字符串a(chǎn)的長度是3,而字符串b的長度是6.

sizeof并不根據(jù)末尾的結(jié)束字符來計(jì)算大小。

例子2:

#include

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;

}

在“小端”CPU上結(jié)果為:0xabab。在“大端”CPU上應(yīng)該為:0x0112.

例子3:

#include

#include

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;

}

結(jié)果為:2,5

(此題與“大小端”無關(guān)。)

判斷CPU是大端還是小端的方法有有多種:

#include

#include

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;

}

方法2:

#include

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;

}

3.C語言中的位域

先看幾個(gè)例子:

#include

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.s.m?=?3;

//?011

printf("0x%x\n",?r.c);

return

0;

}

gcc -o union

union.c

./union

結(jié)果:0x1d (== 0001 1101

==?011?10?1)

#include

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[])

{

d.b?=?100;

//100 ==?0110 0100

printf("0x%x\n0x%x\n0x%x\n",?d.x.a1,?d.x.a2,?d.x.a3);

return

0;

}

gcc -o union2

union2.c

結(jié)果:

0x0 (==?00)

0x1 (==?001)

0x3 (==?011)

上面兩個(gè)例子的運(yùn)行結(jié)果,似乎都說明:小端機(jī)器中,位域的低位組成數(shù)據(jù)的低位,位域的高位組成了數(shù)據(jù)的高位。

似乎也符合:小端CPU通常采用的是LSB

0位序的慣例。

但是這里有意個(gè)疑問:在大端CPU中,上面兩個(gè)例子的結(jié)果是什么呢?結(jié)果和小端CPU一樣嗎?結(jié)果唯一嗎?

因?yàn)榍懊嫖覀冋f過:“但是大端CPU卻有可能采用LSB

0位序也有可能采用的是MSB

0位序”

總結(jié)

以上是生活随笔為你收集整理的位域 内存 字节序_C语言中的位域、字节序、比特序、大小端(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 这里只有精品22 | 青娱网电信一区电信二区电信三区 | 日韩少妇内射免费播放 | 欧美精品1区 | 香蕉视频网站在线观看 | www色| 亚洲成人手机在线 | 国产欧美日韩三级 | 久久艹免费视频 | 国产福利不卡视频 | 国产日韩欧美中文 | 又黄又爽视频在线观看 | 亚洲免费久久 | 奇米影| 日韩欧美精品中文字幕 | 日韩欧美黄色片 | 国产麻豆精品一区二区 | 97中文在线| 少妇毛片视频 | 一区二区三区视频免费视 | 四虎4hu| 懂色av懂色av粉嫩av分享吧 | 欧美三级手机在线观看 | 日韩成人免费在线视频 | 青青青在线免费 | 男人爽女人下面动态图 | 久久久精品视频在线 | 成人免费看片视频 | 日韩天堂在线观看 | 一区二区高清视频 | 亚洲一区在线视频 | 国产一级精品视频 | 国产欧美综合一区二区三区 | 自拍偷拍色综合 | 男人的天堂在线观看av | 久久精品香蕉视频 | 91丨九色丨丰满 | 香蕉视频亚洲一级 | 风韵丰满熟妇啪啪区老熟熟女 | 短裙公车被强好爽h吃奶视频 | 日韩三级黄色 | 超污视频网站 | 日韩一级成人 | 国产视频一区在线 | 粉嫩视频在线观看 | 日韩国产一区二区三区 | 思思久久99 | 日韩女优在线播放 | 丁香婷婷社区 | 国产精品你懂得 | 91黄瓜| 国产网站免费观看 | 人妻久久一区二区 | 黄视频在线观看免费 | 久久不卡影院 | 动漫美女被到爽流 | 丰满少妇大力进入 | 精品视频网 | 玖玖爱在线观看 | 一区二区黄色 | 91麻豆精品国产91久久久无需广告 | 国产亚洲成人av | 四虎网站| 狠狠人妻久久久久久 | 97人人草| 午夜精品一区二区三区三上悠亚 | 亚洲伦乱 | 国产天堂精品 | 国产精品久久久国产盗摄 | 色女人影院 | 午夜a级片| 国产精品suv一区二区69 | 日本做爰高潮又黄又爽 | 日韩www. | 中文字幕亚洲乱码熟女一区二区 | 男女av网站 | 翔田千里一区二区 | 在线一区不卡 | 黄色亚洲视频 | 青青草手机在线观看 | 九色视频在线观看 | 美女穴穴| 农村搞破鞋视频大全 | 免费成人视屏 | 日本亲与子乱ay中文 | 一边顶弄一边接吻 | 男女做激情爱呻吟口述全过程 | 日韩理论在线观看 | 国产福利在线视频 | 欧美日韩一区二区中文字幕 | 免费欧美一区 | 五月开心播播网 | 女人的洗澡毛片毛多 | 黄色国产网站 | 国产男人搡女人免费视频 | 免费久久网站 | 秋霞网一区二区 | 色综合狠狠操 | 亚洲爱爱av |