当前位置:网站首页>A small problem of bit field and symbol expansion
A small problem of bit field and symbol expansion
2022-07-07 10:22:00 【qq_ forty-two million one hundred and twenty thousand eight hun】
annotation :
Bit field : Some information is stored , It doesn't take up a whole byte , Only a few or one binary bits are required , For example, when storing switching value , Only 0 and 1 Two kinds of state , use 1 Bit binary bit . To save storage space , And easy to handle ,C Language has provided a data structure , be called Bit field or Bit segment .
Writing method of bit field : In the definition of structure , Definition The variable name is followed by a colon plus a limited number of digits .
typedef struct {
short a;
short b;
short c:1; // Bit field , On behalf of the variable c Just assign 1bit Of memory space
short reserved : 15;
}_FORTEST;
problem : There are the following codes
#include<stdio.h>
typedef struct {
short a;
short b;
short c:1; // Use bit fields , Instructs the compiler c Only 1 Bit for storage
short reserved : 15;
}_FORTEST;
int main()
{
_FORTEST f;
f.a = 100; f.b = 200; f.c = 1;
f.c &= 0x01;
printf(" Decimal system :a = %d b = %d c = %d\r\n",f.a,f.b,f.c);
printf(" Hexadecimal :a = %#x b = %#x c = %#x",f.a,f.b,f.c);
return 0;
}
The running result is
Decimal system :a = 100 b = 200 c = -1
Hexadecimal :a = 0x64 b = 0xc8 c = 0xffffffff
among
f.a = 100; f.b = 200; f.c = 1;
because f.c Occupy 1 position , therefore f.c The complement of is 1
f.c &= 0x01;
This statement makes f.c And 0x01 Meet each other , The result is 0x1( because f.c Only occupy 1 position )
printf(" Decimal system :a = %d b = %d c = %d\r\n",f.a,f.b,f.c);
printf(" Hexadecimal :a = %#x b = %#x c = %#x",f.a,f.b,f.c);
Output f.c when : It's all about f.c Extended to 4 byte (32bit), And then f.c Only 1bit, The only remaining data bits replace the symbol bits for symbol expansion , So the binary complement after expansion is 32 individual 1, That is to say 10 It's binary -1, as well as 16 It's binary 0xffffffff
边栏推荐
猜你喜欢
How to cancel automatic saving of changes in sqlyog database
Google colab loads Google drive (Google drive is used in Google colab)
Es classes and objects, prototypes
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
ORM--查询类型,关联查询
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
JMeter about setting thread group and time
虚数j的物理意义
基于gis三维可视化技术的智慧城市建设
随机推荐
Introduction to energy Router: Architecture and functions for energy Internet
Why does the starting service report an error when installing MySQL? (operating system Windows)
ISP、IAP、ICP、JTAG、SWD的编程特点
Phpcms realizes PC website access to wechat native payment
The landing practice of ByteDance kitex in SEMA e-commerce scene
Postman interface test II
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
基于gis三维可视化技术的智慧城市建设
Using keras in tensorflow to build convolutional neural network
Some thoughts on the testing work in the process of R & D
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
高数_第1章空间解析几何与向量代数_向量的数量积
Mongodb creates an implicit database as an exercise
单片机(MCU)最强科普(万字总结,值得收藏)
大整数类实现阶乘
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
VS Code指定扩展安装位置
根据设备信息进行页面跳转至移动端页面或者PC端页面
ORM模型--数据记录的创建操作,查询操作
STM32 ADC and DMA