当前位置:网站首页>C language - bit segment
C language - bit segment
2022-07-06 08:03:00 【Orange cat】
Bit segment
- What is a bit segment ?
C Language allows bits in a structure (bit) Specifies the memory length of its members in units
Such members in bits are called bit segments . With bit segments, data can be stored in fewer bits .
Example :
above
_a : 2
It means occupy 2 individual bit position_b : 5
It means occupy 5 individual bit position_c : 10
It means occupy 10 individual bit position_d : 30
It means occupy 30 individual bit positionReview your knowledge :
8 bit = 1 byte
4 byte = A plastic surgery (int)
All the members above add up to 47 bit, Is probably The size of two reshapes
Then why is the answer 8 ?
So how does the median segment of the structure in the above example allocate space ?
Bit segment memory allocation
- The members of a segment can be int unsigned int signed int Or is it char ( It belongs to the plastic family ) type
- The space of the bit segment is based on 4 Bytes ( int ) perhaps 1 Bytes ( char ) The way to open up .
- Bit segments involve many uncertainties , Bit segments are not cross platform , Pay attention to portable program should avoid using bit segment
- The number of bits occupied by the bit field cannot exceed the maximum number of bits that the basic type can represent
That is, bit segments cannot be stored across bytes . for example :int It is Zhan 4 Bytes , Then it can only be 32 bit position , It can't be exceeded
Example :
struct S
{
char a:3;
char b:4;
char c:5;
char d:4;
};
int main()
{
struct S n = {
0 };
n.a = 10;
n.b = 12;
n.c = 3;
n.d = 4;
return 0;
}
The illustration :
This is vs 2022 Compiler test data
- I said before
The space of the bit segment is according to 4 Bytes ( int ) perhaps 1 Bytes ( char ) The way to open up
So the first time is to open up 4 individual int Type of space - Bit segment in
A byte
Internal data is used firstLow address
In the use ofHigh address
, Use... From right to left (char type )
If it is int type , It's a plastic surgery from right to left - When there is not enough space left in one space for the next member , It will be wasted
- Don't give it a name in the position segment , Unable to access the bit segment , But the calculation size still takes up space
- The bit segment cannot be & Address fetch operation
The cross platform problem of bit segment
- int It's uncertain whether a bit segment is treated as a signed number or an unsigned number .
- The number of the largest bits in the bit segment cannot be determined
for example :16 The machine is the largest 16,32 The machine is the largest 32, It's written in 27, stay 16 It's a plane
There will be problems with the device . - The members in the bit segment are allocated from left to right in memory , Or right to left allocation criteria have not yet been defined .
- When a structure contains two bit segments , The second segment is relatively large , Cannot hold the remaining bits of the first bit segment , yes
Discard the remaining bits or use , This is uncertain .
- summary : Compared to the structure , Bit segments can achieve the same effect , But it can save a lot of space , But there are cross platform problems
边栏推荐
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
- 灰度升级 TiDB Operator
- CAD ARX 获取当前的视口设置
- Hackathon ifm
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 从 CSV 文件迁移数据到 TiDB
- [Yugong series] February 2022 U3D full stack class 010 prefabricated parts
- 【云原生】手把手教你搭建ferry开源工单系统
- What are the ways to download network pictures with PHP
- Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
猜你喜欢
07- [istio] istio destinationrule (purpose rule)
ESP系列引脚說明圖匯總
Codeforces Global Round 19(A~D)
Step by step guide to setting NFT as an ens profile Avatar
Nacos Development Manual
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
【T31ZL智能视频应用处理器资料】
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
随机推荐
Database basic commands
你想知道的ArrayList知识都在这
Leetcode question brushing record | 203_ Remove linked list elements
升级 TiDB Operator
从 SQL 文件迁移数据到 TiDB
Erc20 token agreement
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
Pangolin Library: control panel, control components, shortcut key settings
The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
Data governance: Data Governance under microservice architecture
Position() function in XPath uses
flask返回文件下载
wincc7.5下载安装教程(Win10系统)
IP lab, the first weekly recheck
C语言自定义类型:结构体
Secure captcha (unsafe verification code) of DVWA range
How to prevent Association in cross-border e-commerce multi account operations?
指针和数组笔试题解析
灰度升级 TiDB Operator