当前位置:网站首页>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
边栏推荐
猜你喜欢
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
IO模型复习
Word自动生成目录的方法
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Win10 installation vs2015
request对象对请求体,请求头参数的解析
随机推荐
Interface test
Google Colab装载Google Drive(Google Colab中使用Google Drive)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
The request object parses the request body and request header parameters
SQLyog数据库怎么取消自动保存更改
虚数j的物理意义
ES类和对象、原型
Inno setup packaging and signing Guide
Postman interface test IV
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
C logging method
STM32 ADC and DMA
Guide de signature du Code Appx
Some thoughts on the testing work in the process of R & D
2022.7.6DAY598
大整数类实现阶乘
根据设备信息进行页面跳转至移动端页面或者PC端页面
Guid主键
Leetcode exercise - 113 Path sum II
Parameter sniffing (2/2)