位域 bit-fields C的重要数据结构
生活随笔
收集整理的這篇文章主要介紹了
位域 bit-fields C的重要数据结构
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
詳見K&R書上的6.9 section
bit-field來源:bit-field是為了節(jié)約存儲空間而創(chuàng)造的一種數(shù)據(jù)結(jié)構(gòu)(to pack several objects into a singlemachine word)bit-field用途:Externally-imposed data formats,such interfaces to hardware devices,also oftenrequire the ability to get a pieces of a word定義方法:struct (tag) {bit-field list ~~~} bit-field variable list;由定義方法可見,bit-field也是以structure的形式組織起來的數(shù)據(jù)結(jié)構(gòu),只不過是以二進(jìn)制進(jìn)行分發(fā)的而已。其中bit-field list表示方法: type fieldnamme:width; width:field的寬度,以bit表示,還是舉個例子來看吧:struct bf{unsigned int is_keyword:1;unsigned int is_extern:1;unsigned int is static:1;}flags;定義了一個bit-field變量flags,它包括3個1-bit的field,訪問bit-field其實和訪問一般數(shù)據(jù)結(jié)構(gòu)的成員一樣,例如:flags.is_static=0;在定義bit-field的時候,有以下幾點要注意(K&R版本的觀點)1、Almost everything about fields is implemention-dependent ! Whether a field mayoverlap a word boundary is implemention-defined.2、Fields can only be declared as intS,for protability,specify signed or unsignedexplicitly.3、unnamed field can be used for padding(沒有名字的field可以用來作為填充或者調(diào)整用)例如:struct bf{unsigned int a:2;unsigned int? :4; /*該4位不能用*/unsigned int b:2;};4、Special width 0 may be used to force alignment at the next word boundary.(如果width=0的話,那么下一個field就被強制存放在下一個內(nèi)存單元里面了),例如:struct bf{unsigned int a:2;unsigned int? :0; /*空的field*/unsigned int b:2; /*從下一個單元開始存放*/};5、Bit-field are not arrays and they do not have addresses , so the operator &can not be applied on them.某些國內(nèi)的教科書上與K&R對field定義規(guī)則的描述還是有些不同,比如譚浩強版本寫道:1) 一個位域必須存儲在同一個字節(jié)中,不能跨兩個字節(jié)。如一個字節(jié)所剩空間不夠存放另一位域時,應(yīng)從下一單元起存放該位域。
?2) 由于位域不允許跨兩個字節(jié),因此位域的長度不能大于一個字節(jié)的長度,也就是說不能超過8位二進(jìn)位。
bit-field來源:bit-field是為了節(jié)約存儲空間而創(chuàng)造的一種數(shù)據(jù)結(jié)構(gòu)(to pack several objects into a singlemachine word)bit-field用途:Externally-imposed data formats,such interfaces to hardware devices,also oftenrequire the ability to get a pieces of a word定義方法:struct (tag) {bit-field list ~~~} bit-field variable list;由定義方法可見,bit-field也是以structure的形式組織起來的數(shù)據(jù)結(jié)構(gòu),只不過是以二進(jìn)制進(jìn)行分發(fā)的而已。其中bit-field list表示方法: type fieldnamme:width; width:field的寬度,以bit表示,還是舉個例子來看吧:struct bf{unsigned int is_keyword:1;unsigned int is_extern:1;unsigned int is static:1;}flags;定義了一個bit-field變量flags,它包括3個1-bit的field,訪問bit-field其實和訪問一般數(shù)據(jù)結(jié)構(gòu)的成員一樣,例如:flags.is_static=0;在定義bit-field的時候,有以下幾點要注意(K&R版本的觀點)1、Almost everything about fields is implemention-dependent ! Whether a field mayoverlap a word boundary is implemention-defined.2、Fields can only be declared as intS,for protability,specify signed or unsignedexplicitly.3、unnamed field can be used for padding(沒有名字的field可以用來作為填充或者調(diào)整用)例如:struct bf{unsigned int a:2;unsigned int? :4; /*該4位不能用*/unsigned int b:2;};4、Special width 0 may be used to force alignment at the next word boundary.(如果width=0的話,那么下一個field就被強制存放在下一個內(nèi)存單元里面了),例如:struct bf{unsigned int a:2;unsigned int? :0; /*空的field*/unsigned int b:2; /*從下一個單元開始存放*/};5、Bit-field are not arrays and they do not have addresses , so the operator &can not be applied on them.某些國內(nèi)的教科書上與K&R對field定義規(guī)則的描述還是有些不同,比如譚浩強版本寫道:1) 一個位域必須存儲在同一個字節(jié)中,不能跨兩個字節(jié)。如一個字節(jié)所剩空間不夠存放另一位域時,應(yīng)從下一單元起存放該位域。
?2) 由于位域不允許跨兩個字節(jié),因此位域的長度不能大于一個字節(jié)的長度,也就是說不能超過8位二進(jìn)位。
總結(jié)
以上是生活随笔為你收集整理的位域 bit-fields C的重要数据结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星Galaxy Book3系列笔记本发
- 下一篇: 三星Galaxy S23独占3.36GH