当前位置:网站首页>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 : 2It means occupy 2 individual bit position_b : 5It means occupy 5 individual bit position_c : 10It means occupy 10 individual bit position_d : 30It 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 byteInternal data is used firstLow addressIn 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
边栏推荐
- [redis] Introduction to NoSQL database and redis
- It's hard to find a job when the industry is in recession
- Pyqt5 development tips - obtain Manhattan distance between coordinates
- Mex related learning
- Interview Reply of Zhuhai Jinshan
- Data governance: Data Governance under microservice architecture
- 22. Empty the table
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- [非线性控制理论]9_非线性控制理论串讲
- 二叉树创建 & 遍历
猜你喜欢

珠海金山面试复盘
![[research materials] 2021 live broadcast annual data report of e-commerce - Download attached](/img/a6/74da2f44c7b6b22fed2f8e41a55988.jpg)
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
![[untitled]](/img/38/bc025310b9742b5bf0bd28c586ec0d.jpg)
[untitled]

Esrally domestic installation and use pit avoidance Guide - the latest in the whole network

wincc7.5下载安装教程(Win10系统)
![08- [istio] istio gateway, virtual service and the relationship between them](/img/fb/09793f5fd12c2906b73cc42722165f.jpg)
08- [istio] istio gateway, virtual service and the relationship between them

Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation

Linked list interview questions (Graphic explanation)

21. Delete data
![[research materials] 2021 China online high growth white paper - Download attached](/img/51/bea6179e4fac88f8b550b4213a2bca.jpg)
[research materials] 2021 China online high growth white paper - Download attached
随机推荐
. Net 6 learning notes: what is NET Core
C语言自定义类型:结构体
[t31zl intelligent video application processor data]
1204 character deletion operation (2)
649. Dota2 Senate
A Closer Look at How Fine-tuning Changes BERT
TiDB备份与恢复简介
21. Delete data
数据治理:元数据管理篇
hcip--mpls
Epoll and IO multiplexing of redis
Qualitative risk analysis of Oracle project management system
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
图像融合--挑战、机遇与对策
Mex related learning
Flash return file download
24. Query table data (basic)
Circuit breaker: use of hystrix
从 TiDB 集群迁移数据至另一 TiDB 集群
Upgrade tidb operator